Type对象获得泛型类型的两个扩展方法
来源:互联网 发布:deform软件 编辑:程序博客网 时间:2024/05/18 01:21
1、定义扩展对象
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: Listlt = 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: }
2、应用示例。
1: Type type = typeof(Dictionary<int, string>);
2: Type[] tS = type.GetGenericType();
这时tS中分别的System.Int32和System.String的Type对象。
- Type对象获得泛型类型的两个扩展方法
- 获得泛型(generic class)参数类型T的实际类型(actual type)
- PHP面向对象中类方法的两种类型:2、对象类型(获得提示)
- Input对象的type类型
- typeof()和GetType()获得类型的Type类型的区别
- 获得泛型类型的具体类型
- 扩展文件大小的两个方法
- 浅析JavaScript获得Object对象(自定义对象)的类型名方法
- 获得对象的另一种方法
- 获得request对象的方法
- 关于两个对象交换的扩展
- 泛型类型Type的详解
- .NET 类型扩展(Type Extension)
- 怎样获得泛型的类型参数
- 获得泛型的参数类型
- C# 扩展对象的方法
- //扩展对象的count方法
- 如何获得当前类的字段,方法参数泛型的类型
- rhel5 安装JDK
- VC常用数据类型使用转换详解
- 杂谈:饮水思源与Java仍在但Sun已死
- 进行ORACLE语句的调优
- 记录个链接
- Type对象获得泛型类型的两个扩展方法
- 如何使用mib2c
- python正则表达式指南
- java 防止SQL注入
- python正则表达式指南
- PKU 1087 网络最大流
- Java 开发原则和常见错误
- iPhone/iPad/iPod tounch中无需注册即可真机运行的方法
- 关于variables declaration 和definition 的一篇文章