匿名类型 - 有什么可区别的特点? [英] Anonymous Types - Are there any distingushing characteristics?
问题描述
这有什么用,来确定一个类型实际上是匿名类型?例如,一个接口等?
Is there anything to use, to determine if a type is actually a anonymous type? For example an interface, etc?
我们的目标是建立类似如下...
The goal is to create something like the following...
//defined like...
public static T Get<T>(this IAnonymous obj, string prop) {
return (T)obj.GetType().GetProperty(prop).GetValue(obj, null);
}
//...
//And then used like...
var something = new { name = "John", age = 25 };
int age = something.Get<int>("age");
或者是一个匿名类型的只是美丽?因为它需要一个新的形状没有什么,以确定它的自我?
Or is that just the beauty of an anonymous type? Nothing to identify it self because it takes a new shape?
注意 - 我知道,你可以写在对象类的扩展方法,但似乎有点矫枉过正,在我看来
Note - I realize that you can write an extension method for the object class, but that seems like a little overkill, in my opinion.
推荐答案
编辑:下面的列表适用于C#匿名类型。 VB.NET具有不同的规则 - 特别是,它可以产生可变的匿名类型(并执行默认情况下)。贾里德指出,该命名风格不同,太注释。基本上,这是所有pretty的脆弱......
The list below applies to C# anonymous types. VB.NET has different rules - in particular, it can generate mutable anonymous types (and does by default). Jared has pointed out in the comment that the naming style is different, too. Basically this is all pretty fragile...
您不能确定它在一个通用的约束,但是:
You can't identify it in a generic constraint, but:
- 这将是一个类(而不是接口,枚举,结构等)
- 这将有<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.compilergeneratedattribute.aspx">CompilerGeneratedAttribute适用于它
- 这将重载Equals,GetHash code和toString
- 这将是在全局命名空间
- 在它不会被嵌套在另一个类型
- 这将是内部的
- 将被密封
- 这将直接来源于
对象
- 这将是通用与许多类型的参数性能。 (您的可以的具有非一般的匿名类型,没有属性,这是一个有点意义,但。)
- 在每个属性都会有一个名字,包括属性名称的类型参数,并且将这种类型的参数,例如中Name属性变型℃的财产;> _姓名
- 每个属性都将是公开的,只读
- 对于每个属性都会有相应的只读私人领域
- ,将没有其它属性或字段
- 将有一个构造采取对应于每种类型的参数的一个参数,以相同的顺序作为类型参数
- 每个方法和属性将有<一个href="http://msdn.microsoft.com/en-us/library/system.diagnostics.debuggerhiddenattribute.aspx">DebuggerHiddenAttribute适用于它。
- 该类型的名称将开始与&LT;>和遏制AnonymousType
- It will be a class (rather than interface, enum, struct etc)
- It will have the CompilerGeneratedAttribute applied to it
- It will override Equals, GetHashCode and ToString
- It will be in the global namespace
- It will not be nested in another type
- It will be internal
- It will be sealed
- It will derive directly from
object
- It will be generic with as many type parameters as properties. (You can have a non-generic anonymous type, with no properties. It's a bit pointless though.)
- Each property will have a type parameter with a name including the property name, and will be of that type parameter, e.g. the Name property becomes a property of type <>_Name
- Each property will be public and read-only
- For each property there will be a corresponding readonly private field
- There will be no other properties or fields
- There will be a constructor taking one parameter corresponding to each type parameter, in the same order as the type parameters
- Each method and property will have the DebuggerHiddenAttribute applied to it.
- The name of the type will start with "<>" and contain "AnonymousType"
很少的,这是由规范但保证, - 因此它可以在编译器的下一个版本的所有变化,或者如果您使用单声道等
Very little of this is guaranteed by the specification, however - so it could all change in the next version of the compiler, or if you use Mono etc.
这篇关于匿名类型 - 有什么可区别的特点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!