C # reflection 1

Source: Internet
Author: User
Function of reflection:
1. You can use reflection to dynamically create instances of the type, bind the type to an existing object, or obtain the type from an existing object.
2. The application needs to load a specific type from a specific program set at runtime, so that reflection can be used for a task.
3. Reflection main applications and class libraries. These class libraries need to know a type definition to provide more functions.

1. DLL to be reflected
Using System;
Namespace Webtest
{
Public class ReflectTest
{
Public ReflectTest (){}
Public string WriteString (string s)
{
Return "Welcome," + s;
}
// Static functions
Public static string WriteName (string s)
{
Return "Welcome," + s;
}
// Functions without Parameters
Public string WriteNoPara ()
{
Return "you are using a non-parameter method ";
}
}
}

Example for reflection-Add the following functions to the ASPNET page:
Public void test1 ()
{
System. Reflection. Assembly ass;
Type type;
Object obj;
Try
{
Ass = System. Reflection. Assembly. LoadFile (@ "d: \ TestReflect. dll"); // you must specify an absolute path.
Type = ass. GetType ("Webtest. ReflectTest"); // The namespace and class name must be used.
System. Reflection. MethodInfo method = type. GetMethod ("WriteString"); // method name
Obj = ass. CreateInstance ("Webtest. ReflectTest"); // The namespace and class name must be used.
String s = (string) method. Invoke (obj, new string [] {"jianglijun"}); // call an instance method
Or: string s = (string) method. Invoke (obj, Object [] parametors = new Object [] {"param "});
Response. Write (s + "<br> ");
Method = type. GetMethod ("WriteName"); // method name
S = (string) method. Invoke (null, new string [] {"jianglijun"}); // call a static method
Response. Write (s + "<br> ");

Method = type. GetMethod ("WriteNoPara"); // method of the instance without Parameters
S = (string) method. Invoke (obj, null );
Response. Write (s + "<br> ");
Method = null;
}
Catch (Exception ex)
{
Response. Write (ex + "<br> ");
}
Finally
{
Ass = null;
Type = null;
Obj = null;
}
}
--------------------------------------------------------------------------------
Specific applications:
Assume that all the classes in another project are compiled into a dll file. Among these classes, there is a class called StringUtil, And the namespace is under HSMP. CommonBasic. Common.
This class has a method:
Public double GetSum (double x, double y)
{
Return x + y;
}
After compilation, the dll file is stored in the path D: \ Test \ HSMP. CommonBasic. dll.
The problem is how to call the GetSum method in the dll file through a program.
You can take the following steps:
Using System. Reflection;
A.
// LoadFrom is used here. Load can be used only after the dll reference is added in this project.
Assembly objAss = Assembly. LoadFrom (@ "D: \ Test \ HSMP. CommonBasic. dll ");
// Full path of HSMP. CommonBasic. Common. StringUtil class
Type t = objAss. GetType ("HSMP. CommonBasic. Common. StringUtil ");
// Dynamically generate StringUtil-like instances
Object obj = System. Activator. CreateInstance (t );
// Parameter information. GetSum requires two int parameters. If the method does not have any parameters, it declares an array with a length of 0.
System. Type [] paramTypes = new System. Type [2];
ParamTypes [0] = System. Type. GetType ("System. Int32 ");
ParamTypes [1] = System. Type. GetType ("System. Int32 ");
// Find the corresponding method
MethodInfo p = t. GetMethod ("GetSum", paramTypes)
// Parameter value. If the called method has no parameters, do not write these
Object [] parameters = new Object [2];
Parameters [0] = 3;
Parameters [1] = 4;
Object objRetval = p. Invoke (obj, parameters); // if no parameter exists, enter null.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.