C # assembly and reflection

Source: Internet
Author: User

. Net reflection mechanism is in the running state. For any class, all attributes and methods of this class can be known. For any object, can call any of its methods and attributes. This dynamically obtained information and the function of dynamically calling methods of objects is called. net reflection mechanism.

. Net reflection mainly provides the following functions:

A. when running the program: dynamically obtain the loaded assembly, dynamically obtain the type (such as class and Interface), and dynamically obtain the type member information (such as method, field, attribute, etc );

B. dynamically create (mainly dynamically create based on the class name or member name string) instances at runtime, and call and access these instance members;


1. Reflection-Assembly object using System. Reflection;

Assembly class indicates an Assembly (. dll/exe)

How to obtain an assembly:

Obtain all the assemblies in the current program domain: AppDomain. CurrentDomain. GetAssemblies ()

Obtain the Assembly of the current object: this. GetType (). Assembly

Load Assembly by path: Assembly. LoadFrom (assPath)

2. Reflection-Type object

Type class. When the program runs, a class corresponds to an object of the Type class. The Type object can be used to obtain all the definition information of the class, such as the attributes and methods of the class.

How to obtain the Type object: 1, 2 is to obtain a single Type object, 3, 4, 5 is to obtain the Type through the Assembly

1. obtain the corresponding Type: Typet = typeof (Person) through the class)

2. Get Type: Typet = p. GetType () through the object ()

3. Obtain the class defined in the Assembly according to the full name of the class: Type = Assembly. GetType ("BLL. Person ")

4. Get all public classes defined in the assembly: Type [] types = assembly. GetExportedTypes ()

5. Get all types defined in the assembly: Type [] types = assembly. GetTypes ()


Reflection-Type member

Attribute:

? Type. Assembly: Get the Assembly object where type is located? Type. FullName: Get the full name of the class corresponding to the type object? Type. Name: get the Name of the class corresponding to the type object? Type. IsArray: determines whether type is an array class? Type. IsEnum: determines whether type is an enumeration class method :? Type. IsAssignableFrom (Type I): determines whether type implements interface I? Type. IsSubclassOf (Type father): determines whether the type inherits father? Type. IsInstanceOfType (objecto): determines whether o is a type instance? Type. GetFiled ("gender"): Get the Field object named gender in type? Type. GetMethod ("SayHi"): Get the method object named SayHi in type? Type. GetProperty ("Age"): gets the property object named Age in type.

3. Reflection-FiledInfo Field object

The FiledInfo class represents a member field (global variable of the class) in a class)

Example: Operation object field

PublicclassDog

{

PublicstringdogName;

PublicintdogAge;

}

Dog dObj = newDog () {dogName = "Xiaohua", dogAge = 1 };

Type dType = dObj. GetType ();

FiledInfo fiDN = dType. GetFiled ("dogName"); // obtain the Field object

String strName = fiDN. GetValue (dObj); // obtain the dogName field value of dObj.

FiDN. SetValue (dObj, ""); // you can specify the dogName field value in dObj.

4. Reflection-PropertyInfo property object

The PropertyInfo class represents an attribute in a class.

For example, Operation Object Attributes

PublicclassDog

{

PublicstringName {get; set ;}

PublicintAge {get; set ;}

}

Dog dObj = newDog () {Name = "Xiaohua", Age = 1 };

Type dType = dObj. GetType ();

PropertyInfo piN = dType. GetProperty ("Name"); // get the property object

String strName = piN. GetValue (dObj); // obtain the dObj Name Attribute Value

PiN. SetValue (dObj, ""); // you can specify the Name attribute value in dObj.


5. Reflection-MethodInfo method object

The MethodInfo class represents a method in a class.

PublicclassDog

{

PublicstringSmile (string name)

{

Return "A smiling dog:" + name;

}

}


Dog dObj = newDog ();

Type dType = dObj. GetType ();

MethodInfo method = dType. GetMethod ("Smile"); // obtain the method object

Object res1 = dObj. Smile ("Wow Haha ~~ "); // * Common call Method

Object res2 = method. Invoke (dObj, newobject [] {""}); // * reflection calls dObj's Smile method

6. Reflection-dynamically create an object

1. objectres = Activator. CreateInstance (Typetype)

A class constructor is called dynamically to create an object. The returned value is the created object. If the class does not have a constructor, an error is returned.
2. Create with Constructor

Publicclass Dog {

Public Dog (stringname, intage) {// constructor code ...}

}

Type dType = typeof (Dog); // gets the Dog class Type object

// Get the constructor object (based on the parameter type array in the parameter list)

ConstructorInfocotr = dType. getConstructor (new Type [] {typeof (string), typeof (int)}); objectresValue = cotr. invoke (newobject [] {"", 2}); // call dObj's Smile Method


7. Reflection-Private member of the called object

Person p1 = new Person ();

Type type = p1.GetType ();

// BindingFlags. Instance indicates that it is an Instance method and is not a static method.

MethodInfom Haha = type. GetMethod ("Haha", BindingFlags. NonPublic | BindingFlags. Instance );

MHaha. Invoke (p1, null );




<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140326/20140326083336172.jpg" alt = "">


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.