获取匿名类型的Intellisence
2010-05-21 10:22
99 查看
来自于:AlexJ
http://blogs.msdn.com/alexj/archive/2007/11/22/t-castbyexample-t-object-o-t-example.aspx
T CastByExample<T>(object o, T example)
So earlier today I was lamenting that an anonymous type can't be shared between functions with Wes Dyer, when he said "Well actually they can..."
Cue me learning something cool.
The first step is to create a seemingly innocent method:
public static T CastByExample<T>(this object o, T example)
{
return (T) o;
}
Seems innocent enough right?
Well it is until you start using it with anonymous types. Imagine you had this function, that returns an anonymous type as object, because that is your only choice:
static object GetAnonymousType()
{
return new { FullName = "Cosmo Kramer" };
}
Normally if you called this function anywhere you wouldn't be able to get at the anonymous type without using reflection... This is where CastByExample<T> comes to the rescue.
If you know the shape of the anonymous type, you use that to do a CastByExample...
object o = GetAnonymousType();
//get the original anonymous type back again
var v = o.CastByExample(new { FullName = "" });
//Use the properties of the anonymous type initialized in another
//function directly !!
Console.WriteLine(v.FullName);
This works because when an anonymous type is used the compiler first checks that one with the same signature (i.e. all fields are the same name and type) hasn't already been used. If one has the same CLR type is used.
Hence if you pass in an example that is the same shape as the original anonymous type to the CastByExample<T>(..) method will get you back to the original anonymous type... and var magic does the rest.
Nifty huh?
来自于:AlexJ
http://blogs.msdn.com/alexj/archive/2007/11/22/t-castbyexample-t-object-o-t-example.aspx
http://blogs.msdn.com/alexj/archive/2007/11/22/t-castbyexample-t-object-o-t-example.aspx
T CastByExample<T>(object o, T example)
So earlier today I was lamenting that an anonymous type can't be shared between functions with Wes Dyer, when he said "Well actually they can..."
Cue me learning something cool.
The first step is to create a seemingly innocent method:
public static T CastByExample<T>(this object o, T example)
{
return (T) o;
}
Seems innocent enough right?
Well it is until you start using it with anonymous types. Imagine you had this function, that returns an anonymous type as object, because that is your only choice:
static object GetAnonymousType()
{
return new { FullName = "Cosmo Kramer" };
}
Normally if you called this function anywhere you wouldn't be able to get at the anonymous type without using reflection... This is where CastByExample<T> comes to the rescue.
If you know the shape of the anonymous type, you use that to do a CastByExample...
object o = GetAnonymousType();
//get the original anonymous type back again
var v = o.CastByExample(new { FullName = "" });
//Use the properties of the anonymous type initialized in another
//function directly !!
Console.WriteLine(v.FullName);
This works because when an anonymous type is used the compiler first checks that one with the same signature (i.e. all fields are the same name and type) hasn't already been used. If one has the same CLR type is used.
Hence if you pass in an example that is the same shape as the original anonymous type to the CastByExample<T>(..) method will get you back to the original anonymous type... and var magic does the rest.
Nifty huh?
来自于:AlexJ
http://blogs.msdn.com/alexj/archive/2007/11/22/t-castbyexample-t-object-o-t-example.aspx
相关文章推荐
- 通过反射获取匿名类型的属性和属性值
- DataGrid 的 ItemDataBound 中如何获取匿名类型数据源的Item
- 从ArrayList中获取匿名类型变量中的属性值
- 选择单一行、Like Select、获取数据库中的maxID、选择特殊的列并创建临时的匿名类型
- 通过反射获取泛型参数数组(Class对象 List类型)
- PHP获取字符串编码类型
- JavaScriptSerializer 中的匿名类型 转json
- SQL获取所有用户名,数据库名、所有表名、所有字段名及字段类型
- 怎么通过反射的方式获取指定变量名称的值呢?比如说一个const类型的常量:const string aa=123
- Unity3D的SystemInfo类,用于获取运行设备硬件信息(CPU、显卡、类型等)
- 获取Windows系统注册的文件类型和图标
- 为匿名类型添加方法
- java script 判断和获取浏览器的类型
- SQL语句来获取一个表的所有列的信息,如,列名、类型、长度等
- 获取输入框内容,数值类型转换问题
- C# Directory.GetFiles()获取多个类型格式的文件
- 如何使用反射技术获取泛型类的真实类型?
- struts2学习笔记(2)---Action中訪问ServletAPI获取Map类型的Servlet元素
- 如何获取指定表的列和数据类型
- 获取IOS的设备类型