动态声明泛型类型实例 [英] Declare a generic type instance dynamically

查看:409
本文介绍了动态声明泛型类型实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能宣布的通用实例不知道该类型在设计时?

例如:

  INT I = 1;
名单< typeof运算(I)GT;名单=新的名单,其中,typeof运算(I)GT;();
 

,其中i的类型可以,而不必做任何事情,

 名单,其中,INT>名单=新的名单,其中,INT();
 

解决方案

如果您不知道在编译时的类型,但你想要的实际类型(即不名单,其中,对象> )的的你是不是在泛型方法/类型与相应类型的参数,那么你必须使用反射。

为了使反射更简单,我有时会介绍我自己的code一个新的泛型类型或方法,这样我就可以打电话,通过反射,但就用普通的仿制药后。例如:

 对象x = GetObjectFromSomewhere();
//我想创建一个列表<>包含现有
//对象,但强类型的正确类型的视
// x的值的类型
MethodInfo的方法=的GetType()GetMethod的(BuildListHelper)。
方法= method.MakeGenericMethod(新类型[] {x.GetType()});
对象列表= method.Invoke(这一点,新的对象[] {X});

// 后来

公众的IList< T> BuildListHelper< T>(T项)
{
    名单< T>名单=新的名单,其中,T>();
    list.Add(项目);
    返回列表;
}
 

当然,你不能这样做非常多的名单之后,如果你不知道类型...这就是为什么这种事情经常跌倒。并非总是如此,虽然 - 我用类似上面有几次,那里的类型系统只是不很让我EX preSS的一切,我需要静态

编辑:请注意,虽然我打电话Type.GetMethod在code以上,如果你要执行它了很多你可能只想把它称为一次 - 毕竟,该方法ISN牛逼不会改变。您可以使其静态的(你可以在上面的情况下),你可能希望把它变成私有了。我把它作为一个公共实例方法在样本code中的GetMethod的电话的简单 - 你需要另外指定合适的绑定标志

Is it possible to declare an instance of a generic without knowing the type at design-time?

Example:

Int i = 1;
List<typeof(i)> list = new List<typeof(i)>();

where the type of i could be anything, instead of having to do:

List<int> list = new List<int();

解决方案

If you don't know the type at compile-time, but you want the actual type (i.e. not List<object>) and you're not in a generic method/type with the appropriate type parameter, then you have to use reflection.

To make the reflection simpler, I've sometimes introduced a new generic type or method in my own code, so I can call that by reflection but then just use normal generics after that. For example:

object x = GetObjectFromSomewhere();
// I want to create a List<?> containing the existing
// object, but strongly typed to the "right" type depending
// on the type of the value of x
MethodInfo method = GetType().GetMethod("BuildListHelper");
method = method.MakeGenericMethod(new Type[] { x.GetType() });
object list = method.Invoke(this, new object[] { x });

// Later

public IList<T> BuildListHelper<T>(T item)
{
    List<T> list = new List<T>();
    list.Add(item);
    return list;
}

Of course, you can't do an awful lot with the list afterwards if you don't know the type... that's why this kind of thing often falls down. Not always though - I've used something like the above on a few occasions, where the type system just doesn't quite let me express everything I need to statically.

EDIT: Note that although I'm calling Type.GetMethod in the code above, if you were going to execute it a lot you'd probably want to just call it once - after all, the method isn't going to change. You may be able to make it static (you could in the case above) and you probably want to make it private too. I left it as a public instance method for the simplicity of the GetMethod call in sample code - you'd need to specify the appropriate binding flags otherwise.

这篇关于动态声明泛型类型实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆