扩展通用List 类型的方法(Extend method for generic List types)

编程入门 行业动态 更新时间:2024-10-27 21:14:24
扩展通用List <>类型的方法(Extend method for generic List<> types)

TL; DL:为通用列表简化method<returnType, ListType>(this List<ListType>, ...)为method<returnType>(this List<anyType>, ...)

我正在寻找一种扩展方法,它允许我在(任何类型的对象)列表中获取所有属性“P”的值,

到目前为止,我得到了这个方法的工作:

public static T[] selectAll<T, U>(this List<U> list, string property) { List<T> r = new List<T>(); // prepare a list of values of the desired type to be returned foreach(object o in list) { Type mt = o.GetType(); // what are we actually dealing with here? (List of what?) <-- This should be the same as Type U IList<PropertyInfo> props = new List<PropertyInfo>(mt.GetProperties()); // Get all properties within that type foreach(PropertyInfo p in props) { if (p.Name == property) // Are we looking for this property? r.Add((T)p.GetValue(o, null)); // then add it to the list to be returned } } return r.ToArray(); }

因为你不能简单地拥有未知的返回类型,所以我知道有必要在方法调用中指明返回类型,例如:

List<Control> SomeListOfControls = ...

string[] s = SomeListOfControls.selectAll< string , Control>("Text");

但是,由于该列表中项目的类型与此方法无关,我想从等式中消除Type变量。 我希望我可以打电话

List<Control> SomeListOfControls = ...

string[] s = SomeListOfControls.selectAll<string>("Text"); < - 你知道该列表是由什么组成的。<

例如。

但我想不出有办法做到这一点。 即使在编译之前,我可以看到

public static T[] selectAll<T>(this List<> list, string property)

是非Unexpected use of an unbound generic name的Unexpected use of an unbound generic name (意思是List<> )。

而List<object>无法注册为各种List<?extends object> ,可以这么说。

如果可能的话,我可以做这项工作吗?

PS:似乎可能有一种“本地”的方式(.net甚至C#一般)从一个集合中检索<T> P的集合,该集合可能具有类型T的属性P - 我无法弄清楚使用select和所有...但如果有:我很乐意了解它:)

TL;DL: simplify method<returnType, ListType>(this List<ListType>, ...) to method<returnType>(this List<anyType>, ...) for generic Lists

I'm looking to write an extension method that allows me to get the values of all properties "P" within a List of (any type of object)

So far, I got this method to work:

public static T[] selectAll<T, U>(this List<U> list, string property) { List<T> r = new List<T>(); // prepare a list of values of the desired type to be returned foreach(object o in list) { Type mt = o.GetType(); // what are we actually dealing with here? (List of what?) <-- This should be the same as Type U IList<PropertyInfo> props = new List<PropertyInfo>(mt.GetProperties()); // Get all properties within that type foreach(PropertyInfo p in props) { if (p.Name == property) // Are we looking for this property? r.Add((T)p.GetValue(o, null)); // then add it to the list to be returned } } return r.ToArray(); }

Because you can't simply have an unknown return type, I understand that it's necessary to indicate the return type in the method call, e.g:

List<Control> SomeListOfControls = ...

string[] s = SomeListOfControls.selectAll<string, Control>("Text");

But, since the Type of the items within that list is rather irrelevant for this method, I want to eliminate the Type variable from the equation. I wish I could simply call

List<Control> SomeListOfControls = ...

string[] s = SomeListOfControls.selectAll<string>("Text"); <-- You know damn well what this List consists of >.<

for example.

But I can't think of a way to do this. Even before compiling I can see that

public static T[] selectAll<T>(this List<> list, string property)

is an Unexpected use of an unbound generic name (meaning List<>).

And List<object> fails to register as an extension for all kinds of List<?extends object>, so to speak.

How, if at all possible, can I make this work?

PS: It seems like there might be a "native" way (.net or even C# in general) to retrieve a collection of <T> P from a collection that may have properties P of type T - I couldn't figure it out using select and all... But if there is: I'd be happy to learn about it :)

最满意答案

看起来你正在寻找非泛型版本的参数 - 无论是IList或IEnumerable将工作

public static T[] selectAll<T>(this IList list, string property){ ... }

Looks like you are looking for non-generic version for parameter - either IList or IEnumerable would work

public static T[] selectAll<T>(this IList list, string property){ ... }

更多推荐

本文发布于:2023-08-07 21:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466044.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   方法   List   Extend   types

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!