Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.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#_Asp.net_Vb.net - Fatal编程技术网

C# 迭代列表中的所有属性

C# 迭代列表中的所有属性,c#,asp.net,vb.net,C#,Asp.net,Vb.net,我有一份(报告)清单。报表有90个属性。我不想写每一个属性来获取属性的值。有什么方法可以从列表中获取属性值吗 例: 您可以使用反射: static readonly PropertyInfo[] properties = typeof(Reports).GetProperties(); foreach(var property in properties) { property.GetValue(someInstance); } 然而,这将是缓慢的 总的来说,一个课程有90个项目是糟

我有一份(报告)清单。报表有90个属性。我不想写每一个属性来获取属性的值。有什么方法可以从列表中获取属性值吗

例:


您可以使用反射:

static readonly PropertyInfo[] properties = typeof(Reports).GetProperties();

foreach(var property in properties) {
    property.GetValue(someInstance);
}
然而,这将是缓慢的

总的来说,一个课程有90个项目是糟糕的设计。

考虑使用字典或重新思考你的设计。

< P>你可以使用反射:

static readonly PropertyInfo[] properties = typeof(Reports).GetProperties();

foreach(var property in properties) {
    property.GetValue(someInstance);
}
PropertyInfo[] props = 
     obj.GetType().GetProperties(BindingFlags.Public |BindingFlags.Static);
foreach (PropertyInfo p in props)
{
  Console.WriteLine(p.Name);
}
然而,这将是缓慢的

总的来说,一个课程有90个项目是糟糕的设计。

考虑使用字典或重新思考你的设计。

< P>我不太流利地说VB.NET,但是你可以很容易地把它翻译成VB.NET。
PropertyInfo[] props = 
     obj.GetType().GetProperties(BindingFlags.Public |BindingFlags.Static);
foreach (PropertyInfo p in props)
{
  Console.WriteLine(p.Name);
}
var properties = typeof(Report).GetProperties();
foreach(var mReport in mReports) {
    foreach(var property in properties) {
       object value = property.GetValue(mReport, null);
       Console.WriteLine(value.ToString());
    }
}

这就是所谓的。您可以在上的.NET中阅读它的用法。我过去常常获取属性列表,并读取值。您可能需要添加各种属性或检查属性,如
PropertyInfo.CanRead
,以准确获取所需的属性。此外,如果您有任何索引属性,则必须相应地将第二个参数调整为
GetValue

我说VB.NET不流利,但您可以轻松地将类似的内容转换为VB.NET

var properties = typeof(Report).GetProperties();
foreach(var mReport in mReports) {
    foreach(var property in properties) {
       object value = property.GetValue(mReport, null);
       Console.WriteLine(value.ToString());
    }
}

这就是所谓的。您可以在上的.NET中阅读它的用法。我过去常常获取属性列表,并读取值。您可能需要添加各种属性或检查属性,如
PropertyInfo.CanRead
,以准确获取所需的属性。此外,如果您有任何索引属性,则必须相应地将第二个参数调整为
GetValue

听起来非常适合反射或元编程(取决于所需的性能)


听起来很适合反射或元编程(取决于所需的性能)


你想用这些值做什么?你想用这些值做什么?