Type对象获得泛型类型的两个扩展方法
2011-04-11 16:38
399 查看
1、定义扩展对象
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
2、应用示例。
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
这时tS中分别的System.Int32和System.String的Type对象。
1: public static class ExtendMethod
2: {
3:
4: public static Type GetSingleGenericType(this Type t)
5: {
6: Type[] ts = GetGenericType(t);
7: if (ts == null) return null;
8: return ts[0];
9: }
10:
11: public static Type[] GetGenericType(this Type t)
12: {
13: if (!t.IsGenericType) return null;
14: List lt = new List();
15: int begin = t.FullName.IndexOf('[');
16: int end = t.FullName.LastIndexOf(']');
17: string str = t.FullName.Substring(begin + 1, end - begin - 1);
18: while(true)
19: {
20: begin = str.IndexOf('[');
21: if (begin < 0) break;
22: lt.Add(Type.GetType(str.Substring(begin + 1, str.IndexOf(',') - 1)));
23: str = str.Remove(0, str.IndexOf(']') + 1);
24: str = str.TrimStart(',');
25: }
26: return lt.ToArray();
27: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
2、应用示例。
1: Type type = typeof(Dictionary);
2: Type[] tS = type.GetGenericType();
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
这时tS中分别的System.Int32和System.String的Type对象。
相关文章推荐
- 41-知识补充(UIPickerView的补充方法,使用self创建对象的好处,id和instancetype区别,KVC底层代码实现,文本框的两个代理方法)
- TypeUtil 提供方法来判断对象或类的类型
- Gson通过借助TypeToken获取泛型参数的类型的方法
- js判断对象类型 typeof与Object.prototype.toString.call方法的区别
- TypeUtil 提供方法来判断对象或类的类型
- c#语法糖代码——自动属性, 匿名类型,对象与集合初始化器,扩展方法
- Java7中脚本引擎的一般用法,共三种方法获得JavaScript引擎:名称、文件扩展名、MIME类型
- Gson通过借助TypeToken获取泛型参数的类型的方法
- C#特性 学习笔记(对象初始化器 匿名类型 扩展方法)
- 父类通过泛型获得子类Class类型 以及Type体系
- JAVA利用泛型返回类型不同的对象方法
- Gson通过借助TypeToken获取泛型参数的类型的方法
- Gson通过借助TypeToken获取泛型参数的类型的方法(转)
- PHP面向对象中类方法的两种类型:2、对象类型(获得提示)
- js中判断一个对象的类型的方法typeof/constructor/instanceof
- Gson通过借助TypeToken获取泛型参数的类型的方法(转)
- 当泛型方法推断,扩展方法遇到泛型类型in/out时。。。
- 谈谈”隐藏类型局部变量、扩展方法、对象与集合初始化器、匿名类型“
- 利用Lambda表达式、扩展方法以及泛型来为对象添加方法
- C#中的自动属性、隐式类型var、对象初始化器与集合初始化器、扩展方法