从C#类型变量初始化通用变量 [英] Initializing a Generic variable from a C# Type Variable
问题描述
public class AnimalContext< T>< p>
{
public DoAnimalStuff()
{
// AnimalType Specific Code
}
}
我现在可以做的是
AnimalContext< Donkey> ; donkeyContext = new AnimalContext< Donkey>();
AnimalContext< Orca> orcaContext = new AnimalContext< Orca>();
但是我需要/想要做的是能够声明一个AnimalContext初始化为只在运行时才知道。例如,
动物a = MyFavoriteAnimal(); //返回一个类的实例
//实现一个动物
AnimalContext< a.GetType()> a_Context = new AnimalContext< a.GetType()>();
_Context.DoAnimalStuff();
这甚至可能吗?我无法在网上找到答案。
这部分的意思是 em>可能:
new AnimalContext< a.GetType()>();
很明显,确切的语法是错误的,我们会去做,但它是在运行时之前不知道类型参数时, 可能在运行时中构造泛型类型的实例。
这个部分的含义不是:
AnimalContext< a。的GetType()> a_Context
也就是说,如果您不知道变量类型是不可能的编译时处的类型参数。泛型是编译时结构,并依赖于在编译时处提供类型信息。鉴于此,如果您在编译时不知道类型,您将失去泛型的所有好处。
现在,要在运行时构造泛型类型的实例当你直到运行时才知道类型时,你可以这样说:
$ $ p $ $ $ $ $ $ var type = typeof(AnimalContext<>)。 MakeGenericType(a.GetType());
var a_Context = Activator.CreateInstance(type);
请注意,编译时类型的 a_context
是 object
。您必须将 a_context
转换为定义您需要访问的方法的类型或接口。通常你会看到人们在这里做的是使用泛型类型 AnimalContext< T>
来实现一些接口(比如 IAnimalContext
)或从非泛型基类(例如 AnimalContext
)继承,它定义了它们需要的方法(这样就可以投射 a_context
添加到接口或非泛型基类中)。另一种方法是使用 dynamic
。但是请记住,在这种情况下,您有 none 的泛型类型的好处。
I have a class that takes a Generic Type as part of its initialization.
public class AnimalContext<T>
{
public DoAnimalStuff()
{
//AnimalType Specific Code
}
}
What I can do right now is
AnimalContext<Donkey> donkeyContext = new AnimalContext<Donkey>();
AnimalContext<Orca> orcaContext = new AnimalContext<Orca>();
But what I need/want to do is be able to declare an AnimalContext initialized to a type that is only known at runtime. For instance,
Animal a = MyFavoriteAnimal(); //returns an instance of a class
//implementing an animal
AnimalContext<a.GetType()> a_Context = new AnimalContext<a.GetType()>();
a_Context.DoAnimalStuff();
Is this even possible? I can't seem to find an answer for this online.
What you mean by this part is possible:
new AnimalContext<a.GetType()>();
Obviously that exact syntax is wrong, and we'll get to that, but it is possible to construct an instance of a generic type at runtime when you don't know the type parameters until runtime.
What you mean by this part is not:
AnimalContext<a.GetType()> a_Context
That is, it is impossible to type a variable as a generic type if you don't know the type parameters at compile-time. Generics are compile-time constructs, and rely on having the type information available at compile-time. Given this, you lose all the benefits of generics if you don't know the types at compile-time.
Now, to construct an instance of a generic type at runtime when you don't know the type until runtime, you can say:
var type = typeof(AnimalContext<>).MakeGenericType(a.GetType());
var a_Context = Activator.CreateInstance(type);
Note that the compile-time type of a_context
is object
. You will have to cast a_context
to a type or interface that defines the methods you need to access. Often what you'll see people do here is have the generic type AnimalContext<T>
implement some interface (say IAnimalContext
) or inherit from a non-generic base class (say AnimalContext
) that defines the methods they need (so then you can cast a_context
to the interface or the non-generic base class). Another alternative is to use dynamic
. But again, keep in mind, you have none of the benefits of generic types in doing this.
这篇关于从C#类型变量初始化通用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!