Https://github.com/StackExchange/dapper-dot-net
Dapper is an extension to idbconnection, and the extension required to use Dapper provides only the Sqlmapper file in its own project. So the idbconnection in the project can use the extension method in the dapper directly, how is this implemented? Baidu only know this is the extension method provided by C #.
How does the extension method work? Look directly at the code.
Write a custom ToString () method to object that outputs the ToString () method of the custom object
public static string ToString (this Object s,int a)
{
Return the ToString () method of the custom object;
}
The method must be a static method, the parameter must be prefixed with this, and then the class that needs to be extended. An int a parameter is added here if the custom extension method is never called because the custom extension method has the same name and argument as the original method.
Extension methods can also extend their new classes, such as using the
Create a new custom class MyClass
public class MyClass
{
}
Add extension method to MyClass, output the full name of the class
public static string Tomyclassname (this MyClass c)
{
Return C.gettype (). Fullname.tostring ();
}
Such
MyClass my = new MyClass ();
System.Console.WriteLine ("My custom Class" + My.) Tomyclassname ());
The "My custom Class Simple.Console.MyClass" will be output directly
Attach Case Code
usingSystem;namespacesimple.console{ Public classMyClass {} Public Static classExtension//a static class must first be declared, and the class name is arbitrarily { Public Static stringTomyword ( ThisString str)//the extension method must be a static method, the parameter must contain the This keyword, the type after the This keyword is the type that needs to be extended { return "This is my custom string extension method"; } Public Static stringTocurenttime ( ThisDateTime DT) { returnDateTime.Now.ToLongDateString (); } Public Static stringTomyclassname ( ThisMyClass c) { returnC.gettype (). Fullname.tostring (); } Public Static stringToString ( ThisObject S,inta) {return "the ToString () method of the custom object"; } } classProgram {Static voidMain (string[] args) {MyClass my=NewMyClass (); System.Console.WriteLine ("my custom class"+My. Tomyclassname ()); System.Console.WriteLine ("My custom string extension method"+"we are". Tomyword ()); System.Console.WriteLine ("My custom DateTime extension method"+NewDateTime (). Tocurenttime ()); System.Console.WriteLine ("I customize the override ToString Method"+ My. ToString (1)); System.Console.ReadLine (); } }}View Code
Output results
Finally look at the code in the SqlMapper.cs
1 /// <summary>2 ///Execute parameterized SQL3 /// </summary>4 /// <returns>Number of rows affected</returns>5 Public Static intExecute ( Thisidbconnection CNN, commanddefinition command)6 {7 returnExecuteimpl (CNN,refcommand);8}View Code
This is to know that this is the way the IDbConnection interface is extended, and then used in the project, you can directly use the extension method written here.
Dapper Study notes (i)