1
我遇到了一个我似乎无法解决的问题。使用类继承创建模板类型的实例
假设我有设置像这样的类:
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 2013-02-10 16:08:53