Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 枚举所有属性及其值_C#_.net - Fatal编程技术网

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));