C # 3.0 Features

Source: Internet
Author: User
Tags extend hasproperty static class visual studio

In our programming life we have to use a lot of class libraries, some of which we developed ourselves, we have her code, some Third-party release, we have not only their code, not even the opportunity to see.

As a. NET programmer, we have to deal with BCL (Base Class linbrary) every day. Undoubtedly, BCL as a young Framework class library, she is successful, but there are still some times we have to write a "Helper" method to extend the class library, because we can not modify the source code of the class library, we have to write a static class. Although it is convenient to use, but as the pursuit of the perfect programmer is always a bit indecent. Now I have this kind of thing, two days ago was instructed to write a way to load the chart diagram from the XML file, bind the data from the XML to the object, which is definitely a reflection. I often need to write something based on the object's property name to determine whether the object has this property or get the value of the property based on the property name. As usual, I quickly wrote a propertyhelper with two static methods: Hasproperty,getvaluebyname.

Propertyhelper.hasproperty (Point, "X"), so the call is passable, but in C # 3.0 Microsoft provides us with the extension method. Now we can call point directly like this. Hasproperty ("X"); 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, in. NET all classes inherit from object, that all classes have this method by default, really convenient, hehe.

Notice how the difference is with the normal static method? The first argument in this method is preceded by a more this keyword.

Extension methods:

1 the class in which the method is located must be static

The 2 method must also be static

The first parameter of the 3 method must be the type you want to extend, for example, if you want to extend a method to an int, then the first argument must be int.

4 You also need to have a this keyword before the first argument.

By following the steps above you get an "extension method" that you can invoke as the native method of calling this class:

String str = "ABC";
Object len = str. Getvaluebyname ("Length");

It seems that the string type now has the same method as Getvaluebyname, but there is actually no such a method for string. Then why is that? Is our lovely compiler in which to do the hands and feet. To avoid compiler interference, let's directly appreciate the MSIL code:

L_0008:ldstr "Length"
L_000d:call Object Testlambda.propertyextension::getvaluebyname (object, String)

As we can see from MSIL, this code is not any different from the call to the static method after it has been compiled (from the call instruction, which is calling a static method).

From here you can see how the extension method can be invoked using an instance or directly by using static classes:

str.GetValueByName("Length");

PropertyExtension.GetValueByName(str,"Length");

The following are some details about the extension method:

Visual Studio 2008 has IntelliSense support for extension methods, as shown in the following illustration:

One of the icons on the method is not the same as the others, and his mutation has a blue downward arrow below it, which indicates that this method is an extension method.

Here are a few principles to note for writing extension methods (of course, the beholder, the benevolent, this is also opinion):

The extension method has the nearest principle, that is, if you have two identical extension methods in your program, one is in the same namespace as your class, and the other is in a different namespace, which takes precedence over the extension method in the same namespace, which means that the closer the "blood relationship" is, the more favored it is.

Many people see the expansion method may be in the eyes of gold, later in the design regardless of 3,721, can be expanded anyway. Others will expand the class arbitrarily, using an extension method to replace some of the previous "Helper" methods, noting that the extension method is "polluting," so I think it's worth expanding.

When expanding, do not extend to higher-level classes, as I do in the face of object extensions I think is undesirable, object is the base class of all classes, once extended, all classes are "contaminated".

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.