C # extension Method--An advanced Chapter eight: type class extension

Source: Internet
Author: User
Tags assert bool datetime extend static class


Type class provides a large number of properties and methods, but in some basic development work, the type class functionality is somewhat deficient, especially when dealing with generic types, such as nullable types and generic collection types. The following classes extend to these places.

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<>)));
9 public static Type Getnonnullabletype (this type type)
Ten {
-one if (Isnullabletype (Type))
12 {
return type. GetGenericArguments () [0];
return type;

is public static bool Isenumerabletype (this Type enumerabletype)
-return (Findgenerictyp E (typeof (Ienumerable<>), enumerabletype)!= null);

public static type GetElementType (the This Type enumerabletype)
-type type = Findgenerict Ype (typeof (Ienumerable<>), enumerabletype);
if (type!= null)
return type. GetGenericArguments () [0];
return enumerabletype;

The public static bool Iskindofgeneric (the This type, type definition)
% {
GenericType (definition, type)!= null);
public static Type Findgenerictype (the This type definition, type type)
M {
while (type!= NULL) && (type!= typeof (object))
, type. Isgenerictype && (type. GetGenericTypeDefinition () = = definition)
is the return type;
-if (definitio N.isinterface)
(type type2 in type). Getinterfaces ())
Type type3 = findgenerictype (definition, type2);
if (Type3!= NULL)
= type. BaseType;
return null;

From the first name to the general knowledge of the method, the following is part of the test code to help you understand:

1 [TestMethod ()]
2 public void Isnullabletypetest ()
3 {
4 assert.areequal (True, Typeextension.isnul Labletype (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));
9 [TestMethod ()]
is public void getnonnullabletypetest ()
One {
Assert.AreEqual (typeof (int), Typeextens Ion. Getnonnullabletype (typeof (int)));
Assert.AreEqual (typeof (DateTime), Typeextension.getnonnullabletype (typeof (Nullable<datetime>)));
[TestMethod ()]
is public void Isenumerabletypetest ()
$ {
Assert.AreEqual (true, Typeextension.isen Umerabletype (typeof (Ienumerable<string>)));
Assert.AreEqual (True, Typeextension.isenumerabletype (typeof (Collection<int>));
[TestMethod ()]
public void getElementtypetest ()
Assert.AreEqual {
-typeof (int), Typeextension.getelementtype (typeof (ienumerable< int>)));
Assert.AreEqual (typeof (DateTime), Typeextension.getelementtype (typeof (Collection<datetime>)));
[TestMethod ()]
is public void iskindofgenerictest ()
Assert.AreEqual (true, Typeextension.iskin Dofgeneric (typeof (List<string>), typeof (Ienumerable<>));
to Assert.AreEqual (true, Typeextension.iskindofgeneric (typeof (String), typeof (Icomparable<>)));
[TestMethod ()]
is public void Findgenerictypetest ()
$ {
Assert.AreEqual (typeof (Ienumerable<st ring>),
Notoginseng Typeextension.findgenerictype (typeof (Ienumerable<>), typeof (List<string>)));

The code is the best document, presumably everyone has already read it.

Typehelper is a class library I extracted from, it was originally a internal static class, the internal method is also internal static. I just changed the internal to public and added this to the first parameter of each method, and then sorted the methods in the class in order of simplicity to difficulty.

Perhaps because Typehelper is an internal class, and there is a strong application context, Typehelper does not use the way of contract programming, even in the name of some omitted: such as Isenumerabletype, GetElementType Two methods are actually used to handle generic collection types, but there is no word "Generic" on the method name and parameter names. (If you pass in a non-generic type or other type, you will have unpredictable results or exceptions.) )

As simple as this, it is problematic to expose static methods in the internal static class Typehelper as an extension method, so you have to do some work before you apply it. It is not difficult to improve the way the contract is programmed, but it is difficult to give a clear and straightforward name to the extension method, otherwise do not extend it. Isenumerabletype, GetElementType should be reflected in the generic collection to operate, simply add generic words after the name is too long, also difficult to understand. (If you can come up with a good name, please send in the reply, thank you!) )

Also in use, I found two "strange" places, the following figure:

Debug here, Type1 is empty, type2 is a rare strange type.

The source of Typehelper I will explain in the next essay.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.