简体   繁体   English

如何创建列表 <unknown type at compile time> 并通过System.Reflection.PropertyInfo复制项目

[英]How to create a List<unknown type at compile time> and copy items via System.Reflection.PropertyInfo

I have come across something pretty complex. 我遇到过一些非常复杂的事情。 I would be obliged if anyone can help. 如果有人能提供帮助,我将不得不承担责任。

1) I have to create a List<> of unknown type at compile time. 1)我必须在编译时创建一个未知类型的List <>。 That I have already achieved. 我已经实现了。

 Type customList = typeof(List<>).MakeGenericType(tempType);
 object objectList = (List<object>)Activator.CreateInstance(customList);

"temptype" is the custom type thats been already fetched. “temptype”是已经获取的自定义类型。

2) Now I have PropertyInfo object which is that list from which I have to copy all items to the the instance that I have just created "objectList" 2)现在我有PropertyInfo对象,我必须将所有项目复制到我刚刚创建的实例“objectList”

3) Then I need to iterate and access the items of "objectList" as if it were a "System.Generic.List". 3)然后我需要迭代并访问“objectList”的项目,就像它是“System.Generic.List”一样。

Cutting long story short, using reflection I need to extract a property that is a list and have it as an instance for further use. 简而言之,使用反射我需要提取一个列表属性并将其作为实例供进一步使用。 Your suggestions would be appreciated. 您的建议将不胜感激。 Thanks in Advance. 提前致谢。

Umair Umair

Many of the .NET generic collection classes also implement their non-generic interfaces. 许多.NET泛型集合类也实现了它们的非泛型接口。 I'd make use of these to write your code. 我会利用这些来编写代码。

// Create a List<> of unknown type at compile time.
Type customList = typeof(List<>).MakeGenericType(tempType);
IList objectList = (IList)Activator.CreateInstance(customList);

// Copy items from a PropertyInfo list to the object just created
object o = objectThatContainsListToCopyFrom;
PropertyInfo p = o.GetType().GetProperty("PropertyName");
IEnumerable copyFrom = p.GetValue(o, null);
foreach(object item in copyFrom) objectList.Add(item); // Will throw exceptions if the types don't match.

// Iterate and access the items of "objectList"
// (objectList declared above as non-generic IEnumerable)
foreach(object item in objectList) { Debug.WriteLine(item.ToString()); }

I came up with something similar. 我想出了类似的东西。 I borrowed the SetProperties() method from NullSkull and wrote a simple method that calls the NullSkull SetProperties() : 我从NullSkull借用了SetProperties()方法并编写了一个调用NullSkull SetProperties()的简单方法:

    public static List<U> CopyList<T, U>(List<T> fromList, List<U> toList)
    {
        PropertyInfo[] fromFields = typeof(T).GetProperties();
        PropertyInfo[] toFields = typeof(U).GetProperties();
        fromList.ForEach(fromobj =>
        {
            var obj = Activator.CreateInstance(typeof(U));
            Util.SetProperties(fromFields, toFields, fromobj, obj);
            toList.Add((U)obj);
        });

        return toList;
    }

...so with one line of code I can retrieve a List<desired class> populated with matching values by name from List<source class> as follows: ...所以使用一行代码,我可以检索List<desired class> ,按照List<source class>名称填充匹配值,如下所示:

List<desired class> des = CopyList(source_list, new List<desired class>());

As far as performance goes, I didn't test it, as my requirements call for small lists. 就性能而言,我没有测试它,因为我的要求需要小列表。

暂无
暂无

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

相关问题 无法将类型&#39;System.Reflection.PropertyInfo&#39;转换为 - Cannot convert type 'System.Reflection.PropertyInfo' to 如何将System.Reflection.PropertyInfo对象转换为其原始对象类型 - How to convert System.Reflection.PropertyInfo object to its original object type 如何最好地处理system.reflection.propertyInfo NULLReferenceExceptions - how to best handle system.reflection.propertyInfo NULLReferenceExceptions 'System.String' 类型的表达式不能用于'System.Reflection.PropertyInfo' 类型的参数 - 'Expression of type 'System.String' cannot be used for parameter of type 'System.Reflection.PropertyInfo' 转换表达式数组 <Func<TModel, object> &gt; []到列表列表 <System.Reflection.PropertyInfo> - Converting array of Expression<Func<TModel, object>>[] to list of List<System.Reflection.PropertyInfo> 如何提取 class object 的 System.Reflection.PropertyInfo 名称作为 csv 文件的第一行 - How to extract System.Reflection.PropertyInfo names of a class object as first row of a csv file 运算符&#39;==&#39;不能应用于操作数System.Reflection.PropertyInfo和&#39;int&#39; - Operator '==' cannot be applied to operands System.Reflection.PropertyInfo and 'int' 反思:如何通过 PropertyInfo 获取类对象 - Reflection: How to get the class object via PropertyInfo 从PropertyInfo检索未知类型的列表 - Retrieve List of unknown type from PropertyInfo System.Reflection-如何获取子类型的PropertyInfo? - System.Reflection - How can I get the PropertyInfo of the sublevel Types?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM