通过反射将First()与泛型集合一起使用

编程入门 行业动态 更新时间:2024-10-11 01:17:42
本文介绍了通过反射将First()与泛型集合一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假定一个声明为以下内容的方法:

Assume a method declared as the following:

public static string GetString<T>(IEnumerable<T> collection, string theProperty)

如何使用反射返回泛型集合中第一个元素的属性theProperty的值?(使用Linq的First()方法).

How can I, using reflection, return the value of property theProperty of the first element in the generic collection? (using Linq's First() method).

谢谢

推荐答案

为简洁起见,省略了错误检查:

Error checking omitted for brevity:

return (string) typeof(T).GetProperty(theProperty).GetValue(collection.First(), null);

更多推荐

通过反射将First()与泛型集合一起使用

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

发布评论

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

>www.elefans.com

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