C# 枚举所有属性及其值
可能重复:C# 枚举所有属性及其值,c#,.net,C#,.net,可能重复: 我可以使用反射来枚举对象的所有属性名和值吗?尝试使用以下方法: MyClass aClass = new MyClass(); Type t = aClass.GetType(); PropertyInfo[] pi = t.GetProperties(); foreach (PropertyInfo prop in pi) Console.WriteLine("Prop: {0}", prop.Name); 希望它能对您有所帮助。尝试使用以下内容: MyClass
我可以使用反射来枚举对象的所有属性名和值吗?尝试使用以下方法:
MyClass aClass = new MyClass();
Type t = aClass.GetType();
PropertyInfo[] pi = t.GetProperties();
foreach (PropertyInfo prop in pi)
Console.WriteLine("Prop: {0}", prop.Name);
希望它能对您有所帮助。尝试使用以下内容:
MyClass aClass = new MyClass();
Type t = aClass.GetType();
PropertyInfo[] pi = t.GetProperties();
foreach (PropertyInfo prop in pi)
Console.WriteLine("Prop: {0}", prop.Name);
希望它能对您有所帮助。可能是以下内容 假设有一个对象strList
PropertyInfo[] info = strList.GetType().GetProperties();
Dictionary<string, object> propNamesAndValues = new Dictionary<string, object>();
foreach (PropertyInfo pinfo in info)
{
propNamesAndValues.Add(pinfo.Name, pinfo.GetValue(strList, null));
}
PropertyInfo[]info=strList.GetType().GetProperties();
Dictionary propNamesAndValues=新字典();
foreach(信息中的PropertyInfo pinfo)
{
Add(pinfo.Name,pinfo.GetValue(strList,null));
}
可能如下所示
假设有一个对象strList
PropertyInfo[] info = strList.GetType().GetProperties();
Dictionary<string, object> propNamesAndValues = new Dictionary<string, object>();
foreach (PropertyInfo pinfo in info)
{
propNamesAndValues.Add(pinfo.Name, pinfo.GetValue(strList, null));
}
PropertyInfo[]info=strList.GetType().GetProperties();
Dictionary propNamesAndValues=新字典();
foreach(信息中的PropertyInfo pinfo)
{
Add(pinfo.Name,pinfo.GetValue(strList,null));
}
试试这个
var properties = myObj.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public);
var propertyNames = properties.Select(p => p.Name);
var propertyValues = properties.Select(p => p.GetValue(myObj, null));
试试这个
var properties = myObj.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public);
var propertyNames = properties.Select(p => p.Name);
var propertyValues = properties.Select(p => p.GetValue(myObj, null));