[英]Generic method to return Dictionary for all its property
I want to write a generic method, which accept a class object of any type and return a keyValuepair for all the properties of that object. 我想编写一个泛型方法,它接受任何类型的类对象,并为该对象的所有属性返回keyValuepair。
public Dictionary<string,string> GetProperties(T classObj)
{
}
Any help on this would be very greatful. 任何有关这方面的帮助都会非常有用。
Using reflection: 使用反射:
public Dictionary<string, object> GetProperties<T>(T classObj)
{
return typeof(T).GetProperties()
.ToDictionary(p => p.Name, p => p.GetValue(classObj, null));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.