У меня проблема, которую я не могу решить.Создание экземпляра типа шаблона с наследованием класса
Пусть у меня есть классы, установленные вверх, как это:
public abstract class GenericCustomerInformation
{
//abstract methods declared here
}
public class Emails : GenericCustomerInformation
{
//some new stuff, and also overriding methods from GenericCustomerInformation
}
public class PhoneNumber : GenericCustomerInformation
{
//some new stuff, and also overriding methods from GenericCustomerInformation
}
Теперь предположим, что у меня есть установка функции вроде этого:
private void CallCustomerSubInformationDialog<T>(int iMode, T iCustomerInformationObject)
{
//where T is either Emails or PhoneNumber
GenericCustomerInformation genericInfoItem;
//This is what I want to do:
genericInfoItem = new Type(T);
//Or, another way to look at it:
genericInfoItem = Activator.CreateInstance<T>(); //Again, does not compile
}
В функции CallCustomerSubInformationDialog<T>
, у меня есть переменная базового типа GenericCustomerInformation
, который я хочу создать с помощью того, что приходит в T
(один из производных типов: либо Emails
, либо PhoneNumber
)
легкой вещь будет использовать кучу if
условий, но я не хочу ничего делать условное, так как это сделает мой код намного больше, чем это должно быть ..
Спасибо! Не знаю, как я пропустил: p – Ahmad