From http://www.cnblogs.com/jiejie_peng/archive/2010/04/08/1707195.html
C #3.0 new feature-Expansion Method
-
- Main Purpose
Measure the test taker's knowledge about the extension of a compiled class method.
-
- Special note
A. When a class is extended, the new class must be modified using public static because the default class is the internal access modifier.
B. This class must have a file scope, that is, it cannot be used as a nested class.
C. Declare the extension method as static
D. The first parameter type of the extension method is the class to be extended, and this keyword must be added before the Parameter
E. the extension class name is very important. When the Net Framework class library is updated, if the updated class library contains the same class name as the static extension class you previously defined, the custom static extension class is hidden.
- Implementation
As follows: Code Add a method to extend the object class. Because all classes in. Net inherit from the object class, you can select the int type at will during the call.
The Code is as follows: 1 Public Static Class Extendobject
2 {
3 Public Static Void Showtypeinfo ( This Object OBJ)
4 {
5 Console. writeline ( " My datatype is {0} " , Obj. GetType (). Name );
6 }
7 }
The code for calling is as follows:
Two call methods are available: instance call and extension class name call.
Code 1 Class Program
2 {
3 Static Void Main ( String [] ARGs)
4 {
5 Int A = 192 ;
6 // Call static methods using class instances
7 A. showtypeinfo ();
8
9 // Call static methods using static class names
10 Extendobject. showtypeinfo ();
11 Console. Read ();
12 }
13 }
Note that the extension class and the extended class are two different classes. Therefore, when class methods in the extension class access the members of the extended class, only members declared as public modifiers can be accessed.
The Code is as follows:
Extended class: Bird
ClassBird
{
Private String_ Btype;
Public StringBtype
{
Get;
Set;
}
}
Extended extendbird
1 Public Static Class Extendbird
2 {
3 Public Static Void Setbirdtype ( This Bird B, String Birdtype)
4 {
5 // Public attribute, accessible
6 B. btype = Birdtype;
7
8 // Compilation errors may occur.
9 _ Btype = Birdtype;
10 }
11 }
-
- Main Purpose
Measure the test taker's knowledge about the extension of a compiled class method.
-
- Special note
A. When a class is extended, the new class must be modified using public static because the default class is the internal access modifier.
B. This class must have a file scope, that is, it cannot be used as a nested class.
C. Declare the extension method as static
D. The first parameter type of the extension method is the class to be extended, and this keyword must be added before the Parameter
E. the extension class name is very important. When the Net Framework class library is updated, if the updated class library contains the same class name as the static extension class you previously defined, the custom static extension class is hidden.
- Implementation
The following code adds a method to extend the object class. Because all classes in. Net inherit from the object class, the int type is randomly selected during the call.
The Code is as follows: 1 Public Static Class Extendobject
2 {
3 Public Static Void Showtypeinfo ( This Object OBJ)
4 {
5 Console. writeline ( " My datatype is {0} " , Obj. GetType (). Name );
6 }
7 }
The code for calling is as follows:
Two call methods are available: instance call and extension class name call.
Code 1 Class Program
2 {
3 Static Void Main ( String [] ARGs)
4 {
5 Int A = 192 ;
6 // Call static methods using class instances
7 A. showtypeinfo ();
8
9 // Call static methods using static class names
10 Extendobject. showtypeinfo ();
11 Console. Read ();
12 }
13 }
Note that the extension class and the extended class are two different classes. Therefore, when class methods in the extension class access the members of the extended class, only members declared as public modifiers can be accessed.
The Code is as follows:
Extended class: Bird
ClassBird
{
Private String_ Btype;
Public StringBtype
{
Get;
Set;
}
}
Extended extendbird
1 Public Static Class Extendbird
2 {
3 Public Static Void Setbirdtype ( This Bird B, String Birdtype)
4 {
5 // Public attribute, accessible
6 B. btype = Birdtype;
7
8 // Compilation errors may occur.
9 _ Btype = Birdtype;
10 }
11 }