为什么GetProperty()获取不了属性
我不太懂反射原理,只知道这几个方法。
平常使用的时候,GetProperty()都能获取值。
但是linq得到的对象IEnumerable<T>就取不到了
如果IEnumerable<T>.GetProperties()就可以把所有值都取出来
但是IEnumerable<T>.GetProperty(name)就永远是null
怎么办呀
我要取出特定的值我该怎么办呀?
谢谢各位大侠。
平常使用的时候,GetProperty()都能获取值。
但是linq得到的对象IEnumerable<T>就取不到了
如果IEnumerable<T>.GetProperties()就可以把所有值都取出来
但是IEnumerable<T>.GetProperty(name)就永远是null
怎么办呀
public static string ContactText<T>(this IEnumerable<T> en, string name, string separator)
{
string result = string.Empty;
if (en is Array)
{
result = ContactEnumerable(en, null, separator);
}
else
{
result = ContactEnumerable(en, name, separator);
}
if (result.Contains(separator))
result = result.Substring(separator.Length);
return result;
}
public static string ContactEnumerable<T>(IEnumerable<T> en, string name, string separator)
{
System.Reflection.PropertyInfo pi = null;
StringBuilder sb = new StringBuilder();
if (name == null)
{
foreach (object o in en)
sb.Append(string.Concat(separator, o));
}
else
{
foreach (var v in en)
{
pi = en.GetType().GetProperty(name);//取不到值
System.Reflection.PropertyInfo[] pis = v.GetType().GetProperties();//能取到所有的属性
System.Windows.Forms.Button btn = new System.Windows.Forms.Button();
pi = btn.GetType().GetProperty("Text");
break;
}
if (pi != null)
{
foreach (var v in en)
{
object o = pi.GetValue(v, null);
if (o == null)
o = string.Empty;
sb.Append(string.Concat(separator, o.ToString()));
}
}
}
return sb.ToString();
}
我要取出特定的值我该怎么办呀?
谢谢各位大侠。
...全文
相关推荐
Java的System.getProperty()方法获取大全
主要介绍了Java的System.getProperty()方法获取大全,罗列了System.getProperty()方法获取各类信息的用法,具有一定的参考借鉴价值,需要的朋友可以参考下
System.getProperty
System.getProperty 通过该方法获取系统属性信息 很好
C#通过属性名字符串获取、设置对象属性值操作示例
主要介绍了C#通过属性名字符串获取、设置对象属性值操作,结合实例形式总结分析了C#通过反射获取对象属性值并设置属性值,获取对象的所有属性名称及类型等相关操作技巧,需要的朋友可以参考下
Java中System.getProperty的使用
Java中System.getProperty的使用
JS使用getComputedStyle()方法获取CSS属性值
经常会用到js来获取元素的CSS样式,由于方法众多,在下面的文章中为大家详细整理下


