有什么可以使用,以确定一个类型是否实际上是一个匿名类型?例如界面等?
目标是创建以下内容......
//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");
Run Code Online (Sandbox Code Playgroud)
或者这只是匿名类型的美丽?没有什么可以自我识别,因为它需要一个新的形状?
注意 - 我意识到你可以为对象类编写一个扩展方法,但在我看来,这看起来有点矫枉过正.