Reflection: Create a trilogy PS (just talk about the basics !), Reflection Trilogy
Step one(Find the portal)
Using System. Reflection; // reference the namespace to be used
There must be a place to start doing everything. No exception, reflection must first find the reflection entrance. For example, Assembly assemble = Assembly. load ("SqlServer"); // reflection entry: Dynamic dll Loading
Step two(Obtain type)
After the DLL is loaded, find the Type based on the complete name of the class, and then give a question: type Type = assemble. getType ("SqlServer. class1 "); // SqlServer. class1 is a class in SqlServer.
After you know the class, you can find all the method names in the class through loop traversal:
Console. writeLine ("************** GetMethods **************"); foreach (MethodInfo method in type. getMethods () {Console. writeLine ("name {0}", method. name );}
Step three(Object)
Next we need to create an object based on the Type obtained in step two. With the oBject, everything will be fine. For example: object oBject = Activator. createInstance (type); // create an object based on the type. Only objects can be created because they do not know the object type.
The next step is to use the object and do some things around the object.
Specific usage:
MethodInfo show1 = type. getMethod ("show1", new Type [] {}); // show1 is a method in the class and show1.Invoke (oBject, null) without parameters; MethodInfo show2 = type. getMethod ("show2", new Type [] {typeof (int)}); // The show2 method with parameters show2.Invoke (oBject, new object [] {22 });
You have to zoom in! '''''''''''''''''''''''''''''''''''''''' ''''' ''' Black Technology
You know, through reflection, you can call private methods of the class. It can only be said that it is also a thief.
MethodInfo show3 = type. GetMethod ("show3", BindingFlags. Instance | BindingFlags. Public | BindingFlags. NonPublic); // call the private method show3.Invoke (oBject, null) through reflection );
The SQL Server. Class1 code is attached:
1 public void show1 () 2 {3 4 Console. writeLine ("method is {0}", this. getType (); 5 6} 7 8 public void show2 (int id) 9 10 {11 12 Console. writeLine ("this is the show2 method (with parameters) {0}", id); 13 14} 15 16 private void show3 () 17 {18 Console. writeLine ("this is a private method {0}", this. getType (); 19}
Similarly, use the reflection operation table structure ----------------------------------------------------------
1. default access modifier for elements in the namespace
Public: Any other code of the same assembly or other assembly that references the Assembly can access this type or member.
Internal: Any code in the same assembly can access this type or member, but other assembly cannot.
2. default access modifiers for members of various types
The remaining modifiers are mainly inherited from this language. The inherited types include class and interface ). Public and internal can also be used for type members.
Private: Code of the same class and structure can access this type and members.
Protected: code in the same class and derived (inherited feature) class can access this type and member.
Protected internal: Any code in the same assembly or any derived class in other assembly can access this type or member.
MSDN prompt:
1) The accessibility of a derived class cannot be higher than its base type. In other words, there cannot be A public class B Derived from internal Class. If this is allowed, A will become A public class, because all protected members or internal members of A can be accessed from the derived class.
2) The accessibility of a member must not be higher than that of its contained type.
3) You can declare class members (including Nested classes and structures) using any of the five access types ).
Interface)
The default access modifier for interface members is public, and the access modifier cannot be displayed.
Class)
The default constructor is a public access modifier.
The Destructor cannot display the access modifier and is a private access modifier by default.
The default access modifier for class members is private;
Enumeration (enum)
By default, enumeration type members are public access modifiers and cannot be displayed with modifiers.
Structure (struct)
The structure member is a private Modifier by default.
The structure member cannot be declared as a protected member because the structure does not support inheritance.
Nested type
The default access modifier of the nested type is private. It is consistent with the default access type of the class and structure members.