Unity C # Reflection

Source: Internet
Author: User
Tags modifiers

What is reflection

Reflection in. NET also enables the ability to understand the internal structure of an object (or assembly) from the outside of an object, even if you don't know what the object (or assembly) is, in addition. NET can also be used to create an object and execute its methods.

Reflection is. NET, through reflection, you can get information about the members and members of each type (including classes, structs, delegates, interfaces, enumerations, and so on) in a program or assembly at run time. With reflection, you can get a sense of each type. In addition, I can create objects directly, even if the type of the object is not known at compile time.

Purpose of Reflection

(1) Use assembly to define and load assemblies, load list modules in an assembly manifest, and find types from this assembly and create instances of that type.
(2) Use the module to understand the assemblies that contain modules, the classes in modules, and so on, and to get all global methods or other specific non-global methods defined on the module.
(3) Use ConstructorInfo to understand the name of the constructor, arguments, access modifiers (such as pulic or private), and implementation details (such as abstract or virtual).
(4) Use MethodInfo to understand the name of the method, the return type, parameters, access modifiers (such as pulic or private), and implementation details such as abstract or virtual.
(5) Use Fiedinfo to understand the name of a field, access modifiers (such as public or private) and implementation details (such as static), and get or set field values.
(6) Add or remove event handlers by using EventInfo to understand the name of the event, the event handler data type, the custom attribute, the claim type, and the reflection type.
(7) Use PropertyInfo to understand the name, data type, claim type, reflection type, and read-only or writable state of the property, and to get or set the property value.
(8) Use ParameterInfo to understand the name of the parameter, the data type, whether it is an input parameter or an output parameter, and the position of the parameter in the method signature.

Namespaces used for reflection

System.Reflection

System.Type

System.Reflection.Assembly

System.Type class

The System.Type class plays a central role in reflection. But it is an abstract base class, type has a derived class corresponding to each data type, we use the method, field, property of the object of this derived class to find all information about that type.

There are three ways to get a type of a given kind.

use the C # typeof operator.
Type t = typeof (String);


Use the object GetType () method.
string s = "Grayworm";
Type t = S.gettype ();


You can also call the static method GetType () of the type class.
Type t = Type.GetType ("System.String");

Properties of the

type class:
       
 name Data type name
        FullName fully qualified names for data types (including namespace names)
         Namespace defines the namespace name for the data type
        IsAbstract Indicates whether the type is abstract type
        isarray   Indicates whether the type is an array
         isclass   Indicates whether the type is a class
        isenum   Indicates whether the type is an enumeration
        isinterface    Indicates whether the type is an interface
         IsPublic Indicates whether the type is public
        issealed Indicates whether the type is a sealed class
        Isvaluetype Indicates whether the type is a value type


Method of the Type class:
GetConstructor (), GetConstructors (): Returns the ConstructorInfo type, which is used to obtain information about the constructor of the class
GetEvent (), GetEvents (): Returns the EventInfo type, which is used to obtain information about the event of the class
GetField (), GetFields (): Returns the FieldInfo type, which is used to obtain information about the field (member variable) of the class
GetInterface (), getinterfaces (): Returns the Interfaceinfo type, which is used to obtain information about the interface implemented by the class
GetMember (), GetMembers (): Returns the MemberInfo type, which is used to obtain information about all members of the class
GetMethod (), GetMethods (): Returns the MethodInfo type, which is used to obtain information about the method of the class
GetProperty (), GetProperties (): Returns the PropertyInfo type that is used to obtain information about the properties of the class


These members can be called by invoking the InvokeMember () method of type, or by invoking the Invoke () method of MethodInfo, PropertyInfo, and other classes.

The use of System.Type
123456789 Type t = typeof(Rubbit);foreach (ConstructorInfo constructor in t.GetConstructors()){     foreach (ParameterInfo parameter in constructor.GetParameters())     {          Console.WriteLine(parameter.Name + " : "+ parameter.ParameterType.ToString());     }}

Creating an instance object using a constructor variable obtained from reflection

12345678910111213141516 // Get the type variable by rubbit class name type    t   = typeof (Rubbit);  //sets the type array of parameters of the constructor type[] pt   =  new  type[1]; pt[0] = typeof ( int ) ;  //gets the constructor of the corresponding type by the type array constructorinfo con = t. GetConstructor (PT);  //to get rubbit instance object object[] PV = {3} through corresponding pass parameters; rubbit o = con. Invoke (PV) as Rubbit;  //invoke an instance method of the O instance Object Description (); o.description ();

generating an object using the static CreateInstance method of the Activator sealed class

123456789 //通过Rubbit类名获取Type变量Type    t   = typeof(Rubbit);//通过对应的传递参数,得到Rubbit的实例对象object[] pv = { 3 };Rubbit o = Activator.CreateInstance(t,pv) as Rubbit;//调用o实例对象的实例方法Description();o.Description();

 viewing properties in a class

12345678 //通过Rubbit类名获取Type变量Type    t   = typeof(Rubbit);foreach (PropertyInfo property in t.GetProperties()){    //输出属性的类型和变量名    Console.WriteLine(property.Name + " : "+ property.PropertyType.ToString());}

Unity C # Reflection

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.