我有一些示例代码如下:
var items = new List<object>();
var testObjectOne = new
{
Valueone = "test1",
ValueTwo = "test2",
ValueThree = "test3"
};
var testObjectTwo = new
{
Valueone = "test1",
ValueTwo = "test2",
ValueThree = "test3"
};
items.Add(testObjectOne);
items.Add(testObjectTwo);
foreach (var obj in items)
{
var val = obj.Valueone;
}
但是我无法访问Valueone并得到错误:object'不包含'Valueone'的定义,并且没有扩展方法'Valueone'接受类型'object'的第一个参数可以找到(你错过了吗?使用指令或程序集引用?)
问题:我如何遍历此列表并访问ValueOne?非常感谢任何帮助或意见,谢谢
答案 0 :(得分:6)
您可以使用反射来获取匿名类型的属性:
var items = new List<object>();
var testObjectOne = new
{
Valueone = "test1",
ValueTwo = "test2",
ValueThree = "test3"
};
var testObjectTwo = new
{
Valueone = "test1",
ValueTwo = "test2",
ValueThree = "test3"
};
items.Add(testObjectOne);
items.Add(testObjectTwo);
foreach (var obj in items)
{
var val = obj.GetType()
.GetProperty("Valueone")
.GetValue(obj);
}
答案 1 :(得分:4)
另一种没有反射的解决方案是创建一个匿名对象列表,如:
var items = Enumerable.Empty<object>().Select(o=>{
Valueone = "test1",
ValueTwo = "test2",
ValueThree = "test3"
}).ToList();
修改:将对象添加到列表中
items.Add(new {
Valueone = "Value one",
ValueTwo = "Value two",
ValueThree = "Value three"
});
然后在for
内,语句var val = obj.Valueone
将成功编译。
答案 2 :(得分:3)
为什么不创建一个表示这些对象的类?=!它会使您的代码更加稳固,您可以避免slow/costly reflection或unsafe /bad practice dynamic杂技:
public class TempObject
{
public string Valueone { get; set; }
public string ValueTwo { get; set; }
public string ValueThree { get; set; }
}
var items = new List<TempObject>();
var testObjectOne = new TempObject
{
Valueone = "test1",
ValueTwo = "test2",
ValueThree = "test3"
};
items.Add(testObjectOne);
foreach (var obj in items)
{
var val = obj.Valueone;
}
答案 3 :(得分:2)
只需创建匿名类型的列表,而不是“List:
”var list = new[] { new
{
Valueone = "test1",
ValueTwo = "test2",
ValueThree = "test3"
},
new
{
Valueone = "test1",
ValueTwo = "test2",
ValueThree = "test3"
}
}.ToList();
您甚至可以在列表中添加新元素:
list.Add(new {
Valueone = "test1",
ValueTwo = "test2",
ValueThree = "test3"
});
现在您可以使用var
轻松迭代该数组:
foreach(var e in list) ...
根本不需要反思,一切都是强类型的。
答案 4 :(得分:0)
//Anonymous object in C#
var any = new { UserName = "Adel@gmail.com", Password = "P@ssw0rd" };
//Array of anonymous objects in C#
object[] defaultUsers = {
new {UserName = "Adel@gmail.com", Password = "P@ssw0rd" },
new {UserName = "Mike@gmail.com", Password = "P@ssw0rd" },
new {UserName = "John@gmail.com", Password = "P@ssw0rd" }
};
//Iterate through a list of anonymous objects in C#
foreach (var user in defaultUsers)
{
var userName = user.GetType().GetProperty("UserName").GetValue(user);
var password = user.GetType().GetProperty("Password").GetValue(user);
}
这样您可以更好地为数据建立模型
public class DefaultUser
{
public string UserName { get; set; }
public string Password { get; set; }
public string Role { get; set; }
}
然后用这种方式做,那更干净
DefaultUser[] defaultUsers = {
new DefaultUser() { UserName = "admin@user.com", Password = "P@ssword10", Role = "Admins"},
new DefaultUser() { UserName = "author@user.com", Password = "P@ssword10", Role="Authors"},
new DefaultUser() { UserName = "reader@user.com", Password = "P@ssword10", Role="Readers"}};
foreach (DefaultUser user in defaultUsers)
{
var userName = user.UserName;
var password = user.Password;
}