WEB开发网
开发学院软件开发C语言 c#扩展方法奇思妙用高级篇八:Type类扩展 阅读

c#扩展方法奇思妙用高级篇八:Type类扩展

 2010-09-30 21:06:23 来源:WEB开发网   
核心提示: 从名字上就以大体知道方法的功能,下面是部分测试代码,c#扩展方法奇思妙用高级篇八:Type类扩展(2),帮助大家理解: 1[TestMethod()]2publicvoidIsNullableTypeTest()3{4Assert.AreEqual(true,TypeExtension.IsNu

从名字上就以大体知道方法的功能,下面是部分测试代码,帮助大家理解:

 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     }

Tags:扩展 方法 奇思

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接