Case C # extension method is simple to understand not modify the original source code for the class, a method of adding a class. Extension method is defined as static methods , but they are invoked by the instance method syntax. Their first argument specifies the type which acts on the method and the parameters to this modifier prefix.
There is a typical scenario is that the program two open. For example, someone else's DLL is not open source, in order to add a new method in the DLL in a class, it is unlikely. However, extension methods can be used to achieve a similar purpose.
1, two new types of documents: Rectangle, GenericClass.
/// <Summary> /// custom class (rectangle) /// </ Summary> public class the Rectangle { // Properties public Double the Width { GET ; SET ;} = 0 ; // width public Double the Height { GET ; SET ;} = 0 ; // height /// <Summary> /// constructor /// </ Summary> /// <param name = "width"> </ param> /// <param name = " height "> </ param> public Rectangle (double width,double height) { Width = width; Height = height; } /// <summary> /// 求周长 /// </summary> /// <returns></returns> public double GetPerimeter() { return (Width + Height) * 2; } }
/// <summary> /// 范型类 /// </summary> /// <typeparam name="T"></typeparam> public class GenericClass<T> { private T tobj; public GenericClass(T obj) { tobj = obj; } public T GetObject() { return tobj; } }
2, create a WinForm program, add three buttons.
3, the following primary class String, custom categories, paradigm for three types based extension method. Create a new class named: ExtensionHelper.
/// <Summary> /// class is a static class, the method must be public static type, and parameters used this keyword. /// </ Summary> public static class ExtensionHelper { /// <Summary> /// native String Class Extension Method /// </ Summary> /// <param name = "STR"> </ param> public static void the SayHello ( the this String STR) { IF ( String .IsNullOrEmpty (STR)) { MessageBox.Show ( " the Hello World. " , "", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } /// <summary> /// 自定义类扩展方法 /// </summary> /// <param name="mc"></param> /// <returns></returns> public static double GetArea(this Rectangle rect) { return rect.Width * rect.Height; } /// <summary> /// 范型类扩展方法 /// </summary> /// <param name="gen"></param> /// <returns></returns> public static string Show (this GenericClass<string> gc) { return gc.GetObject().ToString(); } }
4, WinForm code is as follows:
/// <summary> /// 原生类String扩展 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { string str = ""; //string str = "Welcom to China."; str.SayHello(); } /// <summary> /// 自定义类扩展 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private voidthe button2_Click ( Object SENDER, EventArgs E) { the Rectangle RECT = new new the Rectangle ( 10 , 10 ); MessageBox.Show ( " area of a rectangle is: " . rect.GetArea + () the ToString (), " prompt " , MessageBoxButtons.OK, MessageBoxIcon.Information); } /// <Summary> /// paradigms class extension /// </ Summary> /// <param name = "SENDER"> </ param> /// <param name = "E "> </ param> private void button3_Click(objectSENDER, EventArgs E) { the GenericClass < String > GC = new new the GenericClass < String > ( " This is a paradigm-based extension method. " ); MessageBox.Show (gc.Show (), " prompt " , MessageBoxButtons.OK, MessageBoxIcon .Information); }
Reference from: https: //www.cnblogs.com/forever-Ys/p/10315830.html