Overview:
This article is the work encountered in the summary of the extension method, good memory is not as messy as the written, first note down, after encountering similar problems, if you forget, you can look at the blog.
First, the problem description:
The problem with the project is to replace the code on the left with the code on the right, and the right code to encapsulate the left-hand code, so the code on the right is simpler.
Datareader.isdbnull (2)? (String) null:dataReader.GetString (2). Trim (); |
Datareader.mygetdatastring (2); |
The type of DataReader is System.Data.SqlClient.SqlDataReader, which is the Framework class library.
and mygetdatastring is a custom method in the project, according to the existing knowledge,datareader.mygetdatastring (2) This line of code at compile time, will not find the definition of the right,
So here is how to call this mygetdatastring method????
Second, the problem analysis
With this problem, let's start by looking at where this method is defined
the My getdatastring method is defined in MyProject. in the Common.Extensions.DataReaderExtensions class
public static string mygetdatastring (this idatarecord datarecorder, int index) { return getdatastring (Datarecorder, index);}
What's so special about this method???
by observing, we find that the parameter list adds a This keyword . This syntax is called an extension method in. NET.
extension method: enables you to "add" methods to an existing type without creating a new derived type, recompiling, or otherwise modifying the original type. An extension method is a special static method, but it can be called just like an instance method on an extended type. For client code written in C # and Visual Basic , there is no significant difference between calling extension methods and calling methods that are actually defined in the type.
Iii. defining and invoking extension methods
1. define a static class to contain extension methods. The class must be visible to client code.
2. Implement the extension method as a static method and make it at least as visible as the containing class.
3. The first parameter of the method specifies the type that the method operates on, and the parameter must begin with the this modifier.
4. in the calling code, add a using directive to specify the namespace that contains the extension method class.
5. Call the extension method in the same way as an instance method on the invocation type.
Iv. examples1. Methods for extending classes
1. Define extension Classes myextension, define extension methods GetMessage
namespace extensionmethods{public static class Myextension {public static string GetMessage (the This string STR) { str = str + "123"; return str;}} }
2. Calling the extension method
Using system;using extensionmethods; Namespace extensionmethods{ class program { static void Main (string[] args) { string s = "abc"; Console.WriteLine (S.getmessage ());}}}
2. Extended interface method
1. define an interface IFly
Namespace extensionmethods{public interface IFly { void Fastfly (); }}
2. define a Duck class to inherit the IFly interface
Using System; Namespace extensionmethods{ class Duck:ifly {public void Fastfly () { Console.WriteLine ("Fast Flay ");}}}
3. define extension Classes flyextension, define extension methods lowspeedfly
Using System; namespace extensionmethods{public static class Flyextension {public static void Lowspeedfly (this IFly Flyinterface) { Console.WriteLine ("Low Speed Fly"); } }}
4. Calling the extension method
Duck Duck = new Duck ();d uck. Lowspeedfly ();
Attention:
1. You can use extension methods to extend a class or interface, but you cannot override extension methods.
2. Extension methods that have the same name and signature as an interface or class method are never called.
3. at compile time, the extension method will always have a lower priority than the instance method defined in the type itself. In other words, if a type has a method named Process (int i) , and you have an extension method with the same signature, the compiler always binds to that instance method. When the compiler encounters a method call, it first looks for a matching method in the instance method of that type. If no matching method is found, the compiler searches for any extension methods defined for the type, and binds to the first extension method it finds.
Extension methods encountered in the project-Summary and sharing