C # features-Expansion Method

Source: Internet
Author: User
In our programming career, we need to use a lot of class libraries. Some of these class libraries are developed by ourselves, we have her code, some are released by third parties, and not only do we not have their code, no chance of viewing.
As A. net programmer, we have to deal with BCL (Base Class Linbrary) every day. Undoubtedly, as a young framework class library, BCL is successful, but sometimes we still have to write some "Helper" methods to extend the class library. Because we cannot modify the source code of the class library, we only need to write static classes one by one. Although it is also convenient to use, but as a perfect programmer, there is always some indecent.
Now I have encountered this kind of thing. I was ordered to write a method for loading Chart settings from an XML file two days ago. Loading data from XML to an object is bound. This must be a useful place for reflection. I often need to write something to determine whether the object has this attribute based on the object property name or to obtain the value of this attribute based on the property name. As usual, I quickly wrote a PropertyHelper with two static methods: HasProperty and GetValueByName.
PropertyHelper. HasProperty (point, "X"), such a call is also successful, but in C #3.0, Microsoft provides us with an extension method. Now we can directly call point. HasProperty ("X") like this to see how I implemented this extension method? Public static class PropertyExtension
{
Public static object GetValueByName (this object self, string propertyName)
{
If (self = null)
{
Return self;
}
Type t = self. GetType ();
PropertyInfo p = t. GetProperty (propertyName );
Return p. GetValue (self, null );
}
}

I added an extension method to the object type. All classes in. net are inherited from the object, and all classes have this method by default. This is really convenient.
What is the difference between the static method and the normal static method? This keyword is added before the first parameter of this method.
Extension Method:
1. The class of the method must be static.
2. The method must also be static.
3. The first parameter of the method must be the type you want to extend. For example, if you want to extend a method to int, the first parameter must be int.
4. You also need the this keyword before the first parameter.
Follow the steps above to write a "Extension Method". You can call this method as you call the native method of this class: string str = "abc ";
Object len = str. GetValueByName ("Length ");

It seems that the string type now has the GetValueByName method, but in fact the string does not have such a method. Why? It's our cute compiler. To avoid compiler interference, let's directly appreciate the MSIL code: L_0008: ldstr "Length"
L_000d: call object TestLambda. PropertyExtension: GetValueByName (object, string)

From MSIL, we can see that the Code after compilation is no different from calling static methods (from the call Command, this is to call a static method ).

Here we can know the extension method, that is, the instance call method or the static class call method can be used directly:

Str. GetValueByName ("Length ");

PropertyExtension. GetValueByName (str, "Length ");
The following describes the extension method in detail:
Visual Studio 2008 supports smart sensing of extension methods, such:

There is a different icon on the method and there is a blue downward arrow under the mutation, which indicates that this method is an extension method.
The following are the principles that need to be paid attention to when writing extension methods (of course, the benevolent and wise are also the words of the family ):
The extension method has the proximity principle, that is, if there are two identical extension methods in your program, one is in the same namespace as your class, in other namespaces, the extension methods in the same namespace are preferred. That is to say, the closer "kinship" is, the more favored it is.
A lot of people may see that the expansion method may have a golden light in their eyes. In the future, it can be expanded regardless of the design. Some people will use extension methods instead of all the previous methods used as "Helper" to expand classes. Note that the extension methods are "contaminated ", therefore, I think it is worthwhile to extend it when it comes to expansion.
During expansion, do not extend the upper-level classes. For example, I think object extension is not desirable. object is the base class of all classes. Once extended, all classes are "contaminated.

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.