c#高级编程之Type类扩展TypeHelper
Type 类提供了大量的属性和方法,但在一些基础性开发工作中,Type类功能还有些欠缺,尤其上在处理泛型类型时,如可空类型和泛型集合类型。下面的类就针对这些地方进行扩展。 Co
Type 类提供了大量的属性和方法,但在一些基础性开发工作中,Type类功能还有些欠缺,尤其上在处理泛型类型时,如可空类型和泛型集合类型。下面的类就针对这些地方进行扩展。

1 public static class TypeHelper 2 { 3 public static bool IsNullableType(this Type type) 4 { 5 return (((type != null) && type.IsGenericType) && 6 (type.GetGenericTypeDefinition() == typeof(Nullable<>))); 7 } 8 9 public static Type GetNonNullableType(this Type type) 10 { 11 if (IsNullableType(type)) 12 { 13 return type.GetGenericArguments()[0]; 14 } 15 return type; 16 } 17 18 public static bool IsEnumerableType(this Type enumerableType) 19 { 20 return (FindGenericType(typeof(IEnumerable<>), enumerableType) != null); 21 } 22 23 public static Type GetElementType(this Type enumerableType) 24 { 25 Type type = FindGenericType(typeof(IEnumerable<>), enumerableType); 26 if (type != null) 27 { 28 return type.GetGenericArguments()[0]; 29 } 30 return enumerableType; 31 } 32 33 public static bool IsKindOfGeneric(this Type type, Type definition) 34 { 35 return (FindGenericType(definition, type) != null); 36 } 37 38 public static Type FindGenericType(this Type definition, Type type) 39 { 40 while ((type != null) && (type != typeof(object))) 41 { 42 if (type.IsGenericType && (type.GetGenericTypeDefinition() == definition)) 43 { 44 return type; 45 } 46 if (definition.IsInterface) 47 { 48 foreach (Type type2 in type.GetInterfaces()) 49 { 50 Type type3 = FindGenericType(definition, type2); 51 if (type3 != null) 52 { 53 return type3; 54 } 55 } 56 } 57 type = type.BaseType; 58 } 59 return null; 60 } 61 }
从名字上就以大体知道方法的功能,下面是部分测试代码,帮助大家理解:

1 [TestMethod()] 2 public void IsNullableTypeTest() 3 { 4 Assert.AreEqual(true, TypeExtension.IsNullableType(typeof(int?))); 5 Assert.AreEqual(false, TypeExtension.IsNullableType(typeof(int))); 6 Assert.AreEqual(true, TypeExtension.IsNullableType(typeof(Nullable<DateTime>))); 7 Assert.AreEqual(false, TypeExtension.IsNullableType(typeof(DateTime))); 8 } 9 [TestMethod()] 10 public void GetNonNullableTypeTest() 11 { 12 Assert.AreEqual(typeof(int), TypeExtension.GetNonNullableType(typeof(int?))); 13 Assert.AreEqual(typeof(DateTime), TypeExtension.GetNonNullableType(typeof(Nullable<DateTime>))); 14 } 15 [TestMethod()] 16 public void IsEnumerableTypeTest() 17 { 18 Assert.AreEqual(true, TypeExtension.IsEnumerableType(typeof(IEnumerable<string>))); 19 Assert.AreEqual(true, TypeExtension.IsEnumerableType(typeof(Collection<int>))); 20 } 21 [TestMethod()] 22 public void GetElementTypeTest() 23 { 24 Assert.AreEqual(typeof(int), TypeExtension.GetElementType(typeof(IEnumerable<int>))); 25 Assert.AreEqual(typeof(DateTime), TypeExtension.GetElementType(typeof(Collection<DateTime>))); 26 } 27 [TestMethod()] 28 public void IsKindOfGenericTest() 29 { 30 Assert.AreEqual(true, TypeExtension.IsKindOfGeneric(typeof(List<string>), typeof(IEnumerable<>))); 31 Assert.AreEqual(true, TypeExtension.IsKindOfGeneric(typeof(string), typeof(IComparable<>))); 32 } 33 [TestMethod()] 34 public void FindGenericTypeTest() 35 { 36 Assert.AreEqual(typeof(IEnumerable<string>), 37 TypeExtension.FindGenericType(typeof(IEnumerable<>), typeof(List<string>))); 38 }
代码就是最好的文档,想必大家已经都看明白了。
精彩图集
精彩文章
热门标签
拖放事件
apache配置文件
onMeasure
mysql(i)_set
TabActivity
页面元素
Execl导出
加减乘除
七牛云服务器
win7_dns服务器
Codernity
网关
rewind
udf编程
遍历对象
强制操作
GUI程序
安
100的阶乘
python正则匹配
connections
嵌套
子类
键盘按键监听
杀进程
遍历map
添加app
pthread_cond
mysql备份策略
性能提示
sbin
thinkphp
选择排序
java格式化数值
noatime
时钟
获取id
excel表格导出
远程脚本
sockets
要被
后台进程管理
MultiLine
limit
请求服务器
版图遍历
很大
切割多层字符
非常
扩展名
分割
scott密码
更改文件名后
生成GUID
双网关
memcachedb
转义符
密匙
ready
合并成一行
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229