简体   繁体   English

从PropertyInfo创建通用列表<Type>

[英]Create Generic List<Type> from PropertyInfo

In one of my classes I have the following property: 在我的一个课程中,我有以下属性:

public List<ClassP> MyPlanes { get; set; }

In another method I have a the propertyInfo of the property above (the function self does not know where the propertyinfo comes from). 在另一个方法中,我有一个上面propertyInfo的propertyInfo(函数self不知道propertyinfo来自哪里)。 Right now i'm trying to create a list of classP when i only have the propertyInfo of MyPlanes . 现在我正在尝试create a list of classP当时我只有MyPlanes So far my attempts seem to be in vain, this is howfar i got sofar: 到目前为止,我的尝试似乎都是徒劳的,这就是我得到的信息:

variable prop is the PropertyInfo of MyPlanes 变量prop是MyPlanes的PropertyInfo

public void GenerateList(PropertyInfo prop)
{
            if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericArguments().Length > 0)
            { 
                List<prop.PropertyType.GetGenericArguments().First()> myValueList = 
                    new List<prop.PropertyType.GetGenericArguments().First()>();
            }
}

The creating of the list (with the code above) gives the following error Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments 创建列表(使用上面的代码)给出以下错误Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments

Any help would be greatly appreciated 任何帮助将不胜感激

Note: I want the list creating in a syntax (or a syntax similar) as visible above. 注意:我希望列表以上面可见的语法(或类似语法)创建。 I want to keep my code as generic as possible 我希望尽可能保持我的代码通用

To make a generic class when you have the type, you can't use the brackets <> because you don't know the type at compile time. 要在具有类型时创建泛型类,不能使用括号<>因为您在编译时不知道类型。 You need to do something like this: 你需要做这样的事情:

var baseType = typeof(List<>);
var genericType = baseType.MakeGenericType(prop.PropertyType.GetGenericArguments().First());

Now you have the correct type and you can create it like so: 现在你有了正确的类型,你可以像这样创建它:

IList myList = (IList)Activator.CreateInstance(genericType);

Edit: although as Selman22 says above, you already have the PropertyType , so you don't need to get the generic type unless you are trying to make a different generic class with the same generic argument (say you have a List<T> and need to make a Dictionary<Type,T> or something). 编辑:虽然正如Selman22上面所说,你已经有了PropertyType ,所以你不需要获得泛型类型,除非你试图用相同的泛型参数创建一个不同的泛型类(比如你有一个List<T>和需要制作一个Dictionary<Type,T>或者其他东西)。 So I'll leave my answer in case you encounter that situation. 所以我会留下我的答案,以防你遇到这种情况。

Unfortunutely, it does not work like that.You need to use Reflection to create a new instance as well: 不幸的是,它不能像那样工作。你需要使用Reflection来创建一个新实例:

var list = (List<ClassP>)Activator.CreateInstance(prop.PropertyType);

CreateInstance method returns object . CreateInstance方法返回object If you don't know the type at compile time, there is no way to cast it. 如果在编译时不知道类型,则无法进行转换。 Best thing you can to would be using dynamic .And that way you can access any members without any compile time error, but clearly it's not safe. 你可以使用dynamic最好的东西。这样你可以访问任何成员而没有任何编译时错误,但显然它不安全。

You should probably reconsider whether you really need to use Reflection here.If your problem can be solved without using Reflection then you should go with that way. 您应该重新考虑一下您是否真的需要在这里使用Reflection 。如果您的问题可以在不使用Reflection情况下解决,那么您应该采用这种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM