Getting Started with C # reflection first understand that C # reflection provides the encapsulation of assemblies, modules, and types of objects, and so on. Then you can use reflection to dynamically create an instance of the type, bind the type to an existing object, or get the type from an existing object and call its methods or access its fields and properties. If you use attributes in your code, you can use reflection to access them.
MSDN Description:
Reflection typically has the following uses:
Use Assembly to define and load assemblies, load modules that are listed in the assembly manifest, and find types from this assembly and create instances of that type.
Use module to discover the following information: Assemblies that contain modules, classes in modules, and so on. You can also get all global methods or other specific non-global methods that are defined on the module.
Use ConstructorInfo to discover the following information: the name of the constructor, arguments, access modifiers (such as public or private), and implementation details such as abstract or virtual. Use the GetConstructors or GetConstructor method of Type to invoke a specific constructor.
Use MethodInfo to find the following information: The name of the method, the return type, parameters, access modifiers (such as public or private), and implementation details such as abstract or virtual. Use the GetMethods or GetMethod method of Type to invoke a specific method.
Use FieldInfo to find the following information: The name of the field, the access modifier (such as public or private) and implementation details (such as static), and Gets or sets the field value.
Use EventInfo to find the following information: The name of the event, the event handler data type, the custom property, the claim type, the reflection type, and so on, and add or remove event handlers.
Use PropertyInfo to find the following information: The name of the property, the data type, the claim type, the reflection type, and the read-only or writable state, and gets or sets the property value.
Use ParameterInfo to find the following information: The name of the parameter, the data type, whether the parameter is an input parameter or an output parameter, and the position of the parameter in the method signature.
When you are working in the reflection-only context of an application domain, use CustomAttributeData to learn about custom properties. With CustomAttributeData, you can examine them without having to create an instance of the property.
The class of the System.Reflection.Emit namespace provides a special form of reflection that enables you to generate types at run time.
Reflection can also be used to create an application called a type browser, which enables the user to select a type and then view information about the selected type.
There are some other uses for reflection. Language compilers such as JScript use reflection to construct symbol tables. Classes in the System.Runtime.Serialization namespace use reflection to access data and determine which fields to persist. The classes in the System.Runtime.Remoting namespace use reflection indirectly through serialization.
one of the simplest examples of C # reflection is to first write the class library as follows:
Using System;
Namespace Reflectiontest
{
public class Writetest
{
Public method with Parametors
public void WriteString (string s, int i)
{
Console.WriteLine ("WriteString:" + S + i.tostring ());
}
static method with only one parametor
public static void Staticwritestring (string s)
{
Console.WriteLine ("Staticwritestring:" + s);
}
static method with no Parametor
public static void Noneparawritestring ()
{
Console.WriteLine ("Noparawritestring");
}
}
}
Compile the csc/t:library ReflectTest.cs command using the command-line compilation to generate the ReflectTest.dll library file.
The following programs are then written:
Using System;
Using System.Reflection;
Class TESTAPP
{
public static void Main ()
{
Assembly;
Type type;
Object obj;
Object any = new object ();
The Assembly.loadfile (@ "D:\Source code\00.c# sudy\01.reflection\01\reflecttest.dll");
Type =. GetType ("Reflectiontest.writetest");
/*example1---------*/
MethodInfo method = Type. GetMethod ("WriteString");
String test = "Test";
int i = 1;
object[] parametors = new object[] {test, i};
obj = the. CreateInstance ("Reflectiontest.writetest");
Method. Invoke (Obj,//instance object of the class need to be reflect
Parametors);
Method. Invoke (Any, parametors);//runtimeerror:class reference is wrong
/*example2----------*/
method = Type. GetMethod ("staticwritestring");
Method. Invoke (NULL, new string[] {"Test"});
Method. Invoke (obj, new string[] {"Test"});
Method. Invoke (Any, new string[] {"Test"});
/*example3-----------*/
method = Type. GetMethod ("noneparawritestring");
Method. Invoke (null, NULL);
}
}
C # Reflection learning when a few things to note :
1. Specifies that the class library file must use an absolute path and cannot use a relative path (in fact it feels a bit unreasonable and inconvenient)
2. 19 rows, the names of namespaces and classes must be specified together
3. In Example 1 you must instantiate the reflection to reflect the class, because the method to be used is not a static method.
4. Since this method has two parameters, you can specify parameters using this Object method or write Method.invoke (obj, new object[] {"Test", 1}) directly;
5. In the example 2 the method we want to use is a static method, when invoke, for the first parameter is ignored, that is, we write nothing will be called, even if we casually new a any such object, of course, this kind of writing is not recommended. But corresponding to the example 1, if you use an instance of type inconsistency when you invoke it, it will cause a runtime error.
6. The third example is an example of invoking a parameterless static method, at which point we do not need to specify the two parameters, which can be used with NULL.
Another problem, if the call class is static class, you need to pay attention to a problem, certainly we will think of a problem, static class can not be instantiated, at this time, the 31-row class of the instantiation of the method we do not need, the direct use of Invoke can be implemented, or there will be a run-time error, In the same way, the first parameter will be ignored, as long as we pass the argument to the right one.
C # Reflection and the content of C # Reflection instances are introduced here to help you understand and learn C # reflection as well as C # reflection instance applications.
C # Reflection example learning and attention content