使用 C# 匿名类型
我正在调用一个返回包含 ac# 匿名类型对象的列表变量的方法。例如:
List<object> list = new List<object>();
foreach ( Contact c in allContacts ) {
list.Add( new {
ContactID = c.ContactID,
FullName = c.FullName
});
}
return list;
如何在我正在处理的代码中引用此类型属性,例如
foreach ( object o in list ) {
Console.WriteLine( o.ContactID );
}
我知道我的示例是不可能的,我只是这样写来表示我需要准确识别匿名类型的每个属性。
谢谢!
解决方案:
不仅仅是一个答案是正确的和/或提出了可行的解决方案。我最终使用了 Greg 答案的选项 3。我学到了一些关于 .NET 4.0 中的动态
的非常有趣的事情!
I am calling a method that returns a List variable that contains a c# Anonymous Type objects. For example:
List<object> list = new List<object>();
foreach ( Contact c in allContacts ) {
list.Add( new {
ContactID = c.ContactID,
FullName = c.FullName
});
}
return list;
How do I reference this type properties in the code I am working on like for example
foreach ( object o in list ) {
Console.WriteLine( o.ContactID );
}
I know that my sample is not possible, I have only wrote that way to say that I need to identify each property of the anonymous type exactly.
Thanks!
Solution:
Not just one of the answer is correct and/or suggest a working solution. I have ended up to using Option 3 of Greg answer. And I learned something very interesting regarding the dynamic
in .NET 4.0!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您不能返回匿名类型的列表,它必须是
对象
的列表。因此您将丢失类型信息。选项 1
不要使用匿名类型。如果您尝试在多个方法中使用匿名类型,请创建一个真正的类。
选项 2
不要将匿名类型向下转换为
object
。 (必须采用一种方法)选项 3
使用动态关键字。 (需要 .NET 4)
选项 4
使用一些肮脏的反射。
You can't return a list of an anonymous type, it will have to be a list of
object
. Thus you will lose the type information.Option 1
Don't use an anonymous type. If you are trying to use an anonymous type in more than one method, then create a real class.
Option 2
Don't downcast your anonymous type to
object
. (must be in one method)Option 3
Use the dynamic keyword. (.NET 4 required)
Option 4
Use some dirty reflection.
仅当列表为
IEnumerable
时,这才有效,如下所示:但您不能返回匿名类型,因为您必须定义返回类型(您不能返回
var) 和匿名类型不能有名称。如果你想传递它,你应该创建非匿名类型。实际上,除了查询内部之外,匿名类型不应该使用太多。
this will work only if list is
IEnumerable<anonymous type>
, like this:but you can't return anonymous types, because you must define return type (you can't return
var
) and anonymous types can't have names. you should create non-anonymous type if you with to pass it. Actually anonymous types should not be used too much, except for inside of queries.如果您有这样的方法:
您实际上不应该这样做,但是有一个 非常丑陋且不面向未来 您可以使用的技术:
它依赖于编译器重用的事实(可以更改!)具有相同“形状”(属性名称和类型)的类型的匿名类型。由于您的“示例”与方法中类型的“形状”匹配,因此会重用相同的类型。
C# 4 中的动态变量是解决这个问题的最佳方法。
If you have a method like this:
You shouldn't really do this, but there's a very ugly and not future-proof technique that you can use:
It relies on the fact (which can change!) that the compiler reuses anonymous types for types that have the same "shape" (properties names and types). Since your "example" matches the "shape" of the type in the method, the same type is reused.
Dynamic variables in C# 4 are the best way to solve this though.
您无法使用匿名类型执行此操作。只需创建一个 Contact 类/结构并使用它。
然后你可以这样做:
...或者这样:
当然,在这种情况下你应该创建一个联系人列表而不是对象列表:
编辑:错过了问题的重要部分。现在已修复。
编辑:再次更改答案。见上文。
You cannot do this with anonymous types. Just create a Contact class/struct and use that.
Then you can do this:
...or this:
Of course you should in that case just do create a Contact list instead of an object list:
EDIT: Missed essential part of the question. Now fixed.
EDIT: Changed answer yet again. See above.
对于每个构造,用 var 替换 object 可能有效
replacing object with var in for each construct may work
我知道我参加聚会迟到了,但我研究了其他东西,发现了这个 文章回答了您的问题。
可以将对象列表强制转换回匿名类型。
I know I'm late to the party but I researching somthing else and found this article which answers your question.
It is possible to cast the list of objects back into the anonymous type.
返回列表
list back
我会使用
,那么你根本不需要声明。
我相信更少的声明、更少的分号会导致更少的错误
i would use
then you dont need to declare at all.
i believe that less declaration, less semi-colon leads to less bug