Jam*_*858 83 c# foreach properties generic-list
我有一个包含几个属性的类(如果它有任何区别,则所有都是字符串).
我还有一个列表,其中包含许多不同的类实例.
在为我的类创建一些单元测试时,我决定循环遍历列表中的每个对象,然后循环遍历该对象的每个属性......
我以为这样做会很简单......
foreach (Object obj in theList)
{
foreach (Property theProperties in obj)
{
do some stufff!!;
}
}
Run Code Online (Sandbox Code Playgroud)
但这没有用!:(我收到此错误...
"foreach语句不能对'Application.Object'类型的变量进行操作,因为'Application.Object'不包含'GetEnumerator'的公共定义"
有没有人知道这样做的方法没有大量的ifs和循环或没有进入任何太复杂的东西?
sll*_*sll 132
尝试一下:
foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties())
{
// do stuff here
}
Run Code Online (Sandbox Code Playgroud)
另请注意,Type.GetProperties()
有一个重载,它接受一组绑定标志,因此您可以过滤掉不同条件(如可访问性级别)的属性,有关详细信息,请参阅MSDN:Type.GetProperties Method(BindingFlags)最后但并非最不重要的是不要忘记添加"system.Reflection"程序集引用.
例如,解决所有公共属性:
foreach (var propertyInfo in obj.GetType()
.GetProperties(
BindingFlags.Public
| BindingFlags.Instance))
{
// do stuff here
}
Run Code Online (Sandbox Code Playgroud)
请告诉我这是否按预期工作.
das*_*ght 31
您可以循环遍历对象的所有非索引属性,如下所示:
var s = new MyObject();
foreach (var p in s.GetType().GetProperties().Where(p => !p.GetGetMethod().GetParameters().Any())) {
Console.WriteLine(p.GetValue(s, null));
}
Run Code Online (Sandbox Code Playgroud)
由于GetProperties()
返回索引器以及简单属性,因此在调用之前需要一个额外的过滤器,GetValue
以便知道null
作为第二个参数传递是安全的.
您可能需要进一步修改过滤器,以便清除只写和其他不可访问的属性.
Gra*_*mas 21
你几乎就在那里,你只需要从类型中获取属性,而不是期望以集合或属性包的形式访问属性:
var property in obj.GetType().GetProperties()
Run Code Online (Sandbox Code Playgroud)
从那里你可以这样访问:
property.Name
property.GetValue(obj, null)
Run Code Online (Sandbox Code Playgroud)
使用GetValue
第二个参数将允许您指定索引值,这将使用返回集合的属性 - 因为字符串是字符集合,您还可以指定索引以在需要时返回字符.
Eri*_*ert 19
好没问题:
foreach(object item in sequence)
{
if (item == null) continue;
foreach(PropertyInfo property in item.GetType().GetProperties())
{
// do something with the property
}
}
Run Code Online (Sandbox Code Playgroud)
我在此页面上寻找类似问题的答案,我写了几个类似问题的答案,可能会对进入此页面的人有所帮助。
List<T>类表示可以通过索引访问的对象列表。它位于 System.Collection.Generic 命名空间下。List 类可用于创建不同类型的集合,如整数、字符串等。List 类还提供搜索、排序和操作列表的方法。
具有属性的类:
class TestClss
{
public string id { set; get; }
public string cell1 { set; get; }
public string cell2 { set; get; }
}
var MyArray = new List<TestClss> {
new TestClss() { id = "1", cell1 = "cell 1 row 1 Data", cell2 = "cell 2 row 1 Data" },
new TestClss() { id = "2", cell1 = "cell 1 row 2 Data", cell2 = "cell 2 row 2 Data" },
new TestClss() { id = "3", cell1 = "cell 1 row 2 Data", cell2 = "cell 2 row 3 Data" }
};
foreach (object Item in MyArray)
{
Console.WriteLine("Row Start");
foreach (PropertyInfo property in Item.GetType().GetProperties())
{
var Key = property.Name;
var Value = property.GetValue(Item, null);
Console.WriteLine("{0}={1}", Key, Value);
}
}
Run Code Online (Sandbox Code Playgroud)
或者,带有字段的类:
class TestClss
{
public string id = "";
public string cell1 = "";
public string cell2 = "";
}
var MyArray = new List<TestClss> {
new TestClss() { id = "1", cell1 = "cell 1 row 1 Data", cell2 = "cell 2 row 1 Data" },
new TestClss() { id = "2", cell1 = "cell 1 row 2 Data", cell2 = "cell 2 row 2 Data" },
new TestClss() { id = "3", cell1 = "cell 1 row 2 Data", cell2 = "cell 2 row 3 Data" }
};
foreach (object Item in MyArray)
{
Console.WriteLine("Row Start");
foreach (var fieldInfo in Item.GetType().GetFields())
{
var Key = fieldInfo.Name;
var Value = fieldInfo.GetValue(Item);
}
}
Run Code Online (Sandbox Code Playgroud)
或者,对象列表(没有相同的单元格):
var MyArray = new List<object> {
new { id = "1", cell1 = "cell 1 row 1 Data", cell2 = "cell 2 row 1 Data" },
new { id = "2", cell1 = "cell 1 row 2 Data", cell2 = "cell 2 row 2 Data" },
new { id = "3", cell1 = "cell 1 row 2 Data", cell2 = "cell 2 row 3 Data", anotherCell = "" }
};
foreach (object Item in MyArray)
{
Console.WriteLine("Row Start");
foreach (var props in Item.GetType().GetProperties())
{
var Key = props.Name;
var Value = props.GetMethod.Invoke(Item, null).ToString();
Console.WriteLine("{0}={1}", Key, Value);
}
}
Run Code Online (Sandbox Code Playgroud)
或者,对象列表(它必须具有相同的单元格):
var MyArray = new[] {
new { id = "1", cell1 = "cell 1 row 1 Data", cell2 = "cell 2 row 1 Data" },
new { id = "2", cell1 = "cell 1 row 2 Data", cell2 = "cell 2 row 2 Data" },
new { id = "3", cell1 = "cell 1 row 2 Data", cell2 = "cell 2 row 3 Data" }
};
foreach (object Item in MyArray)
{
Console.WriteLine("Row Start");
foreach (var props in Item.GetType().GetProperties())
{
var Key = props.Name;
var Value = props.GetMethod.Invoke(Item, null).ToString();
Console.WriteLine("{0}={1}", Key, Value);
}
}
Run Code Online (Sandbox Code Playgroud)
或者,对象列表(带键):
var MyArray = new {
row1 = new { id = "1", cell1 = "cell 1 row 1 Data", cell2 = "cell 2 row 1 Data" },
row2 = new { id = "2", cell1 = "cell 1 row 2 Data", cell2 = "cell 2 row 2 Data" },
row3 = new { id = "3", cell1 = "cell 1 row 2 Data", cell2 = "cell 2 row 3 Data" }
};
// using System.ComponentModel; for TypeDescriptor
foreach (PropertyDescriptor Item in TypeDescriptor.GetProperties(MyArray))
{
string Rowkey = Item.Name;
object RowValue = Item.GetValue(MyArray);
Console.WriteLine("Row key is: {0}", Rowkey);
foreach (var props in RowValue.GetType().GetProperties())
{
var Key = props.Name;
var Value = props.GetMethod.Invoke(RowValue, null).ToString();
Console.WriteLine("{0}={1}", Key, Value);
}
}
Run Code Online (Sandbox Code Playgroud)
OR,字典列表
var MyArray = new List<Dictionary<string, string>>() {
new Dictionary<string, string>() { { "id", "1" }, { "cell1", "cell 1 row 1 Data" }, { "cell2", "cell 2 row 1 Data" } },
new Dictionary<string, string>() { { "id", "2" }, { "cell1", "cell 1 row 2 Data" }, { "cell2", "cell 2 row 2 Data" } },
new Dictionary<string, string>() { { "id", "3" }, { "cell1", "cell 1 row 3 Data" }, { "cell2", "cell 2 row 3 Data" } }
};
foreach (Dictionary<string, string> Item in MyArray)
{
Console.WriteLine("Row Start");
foreach (KeyValuePair<string, string> props in Item)
{
var Key = props.Key;
var Value = props.Value;
Console.WriteLine("{0}={1}", Key, Value);
}
}
Run Code Online (Sandbox Code Playgroud)
祝你好运..