Reflection-optimization and assembly, etc. (the method (or property, field) that requires reflection calls is invoked in a delegate way, without using the Invoke method)

Source: Internet
Author: User

Reflection-optimization and assembly, etc. (the method (or property, field) that requires reflection calls is invoked in a delegate way, rather than using the Invoke method)   Create delegate (1). Delegate.createdelegate (Type, MethodInfo): Creates a static method of the specified type,     1.Type (delegate type)     2.MethodInfo ( The type of the static method to create information through the class. GetMethod (FuncName, Bindingflags.ignorecase | bindingflags.static | BindingFlags.Public)     3. For example: typeof (String). GetMethod ("Equals",  system.reflection.bindingflags.public);(2). Delegate.createdelegate (type,type,string): Creates a delegate of the specified type that represents the specified static method for the specified class     1.Type (delegate type)     2.Type (Type of Class)     3.String (method name) (3). Delegate.createdelegate (type,object,string): Creates a delegate of the specified type that represents the specified instance method to invoke on the specified class instance     1.Type (delegate type)     2.Object (instance of Class)     3.String (method name) (4). Delegate.createdelegate (Type,object,string,bool): Creates a delegate of the specified type that represents the specified instance method to invoke on the specified class instance, case sensitive     1.Type (delegate type )     2.Object (instance of Class)     3.String (method name)     4.bool (igonore case indicates whether casing sensitive)   assembly, reflection, etc. (1 ). C # Compilation principle     1.c# after generating exe file, put in the bin directory. Bin is binary(binary) abbreviation, but the Il file is actually placed in the bin directory instead of the binary file.     2.JIT compiles IL intermediate language into binary mechanical language, but not all at once, but compiles what with what, before compiling is a bunch of strings.     3. The registry records which suffix name is to be opened with which program. This information is registered in the registry when the program is installed. EXE runs with the operating system by default, but because the operating system does not understand IL, it is handed to the FW. The FW runtime will look for the main function first. The default profile (App. config) is not compiled into the EXE, it is not rebuilt after the change, but the program is restarted because it is read only once when it is opened.     4.exe download to memory called assembly. Class corresponds to the object of type in memory   (2). Assembly (Assembly). exe .dll    1. All. NET classes are defined in a assembly (assembly). The net base class is defined in mscorlib.dll. EXE can also be seen as a class library, or can be referenced by: NET EXE is also assembly,. NET in the EXE and DLL is the difference between the EXE contains the entry function, the other no difference, EXE can also be referenced as a DLL, you can also decompile.     2.appdomain.currentdomain.getassemblies ();//Get all the assembly of the program. application domain (AppDomain): A boundary that is established by the common language runtime (Clr-common language runtime) around objects created in the same application scope (that is, from the application entry point, anywhere along the sequence where the object is activated). Application domains help isolate objects created in one application from objects created in other applications so that run-time behavior can be predicted. Multiple application domains can exist in a single process. An application domain can be understood as a lightweight process. Play a safe role. Small resource footprint.     3.system.reflection.assembly.getexecutingassembly ();//Gets the assembly object being executed     4.assembly.load (" Assembly name, which is the name of the DLL, without the. dll suffix ")     5.assembly.loadfrom (" Assembly Physical Path ")     6.assembly.load (""). CreateInstance ("namespace. Class")     Eg:var fullname = assembly. fullname         if (FullName. StartsWith ("Assembly-sharp-editor"))  (3). The type class     1.Type class can be called "Class class," a type object that corresponds to a class of type, and all of the definition information of the class can be obtained by Type objects, such as what properties the class has, which methods, and so on. Type is the description of the class.     2.JIT generates objects of type class: Only when a class is first encountered in a program, the class is loaded, the contents of the class are parsed, the corresponding type object is generated, and the information parsed by the class is loaded into the type object and stored in memory for later reuse.   (4). Some methods of Type     1.bool isassignablefrom (Type c) [is assignable from can be ... Assignment]: Determines whether a variable of the current type can accept the assignment of a C type variable. (whether the C type variable implements the current type, determines whether the C type variable implements the interface)     2.bool IsInstanceOfType (object O): Determines whether the object o is an instance of the current class (the current class can be an O class, parent class, interface)     3.bool issubclassof (Type C): Determines whether the current class is a subclass of Class C     4.bool Isinterface Determines whether the current class is interface     5.typeof (A). BaseType is a direct base class type of a class, not a base class B inherits from A, c inherits from B, then typeof (C). Basetype.fullname = b  (5). Method for getting the Type     1.Type t = typeof (person)     2.Type t = p.gettype ()     3. Through the Assembly GetType () method (Type t = assembly. GetType ("namespace. Class name")) (   if it is taken from an assembly, the parameters inside must be namespace. Class name)     assembly. CreateInstance is a encapsulated Activator.CreateInstance method. The former is more secure in the security of some, in fact, is sentenced to empty (determine whether the type is empty) .    such as, assembly. CreateinstancE ("namespace. Class name") equals type T = assembly. GetType ("namespace. Class name") if (null! = t) {temp = Activator.CreateInstance (t);}     4. * classes are placed in memory as type objects      name: class name     FullName: namespace. Class name     5.type. GetCustomAttributes (typeof (Exportattribute), false) gets   (6). Dynamic Call members     1. The GetProperty method that invokes the type can get Property object PropertyInfo, main member: CanRead, CanWrite, PropertyType, property type: SetValue, GetValue: Read value, Set the value, the first argument is the instance object, because set, get is for the specific instance, and the last parameter is null. Type. GetProperty ("Age"). SetValue (p1, +, null)     2. Calling the GetMethod method of type can get the method object MethodInfo based on the method name, calling the MethodBase invoke method to invoke the method. The first parameter is the instance object, and the second parameter is the parameter array, if no parameter is set to NULL.   (7). Creating Objects     1.activator.createinstance (type T) dynamically invokes the class's parameterless constructor to create an object, and the return value is the object created if the class does not have a parameterless constructor. (App: Create objects dynamically from a configuration file.) )
2. Call the parameter constructor, there are several parameters to pass several dog d=activator.createinstance (t, "Japan") as dog; The purpose of Attribute1.attribute is to provide associated additional information for the element. Among them, "[AttributeUsage (AttributeTargets.Class)]" describes the attribute provides additional information of the element is Class, it can be method, struct, etc. 2. Define a attribute[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]//Here also declares the type of this attribute effectPublic class Versionattribute:attribute{Public string Name {get; set;}Public string Date {get; set;}Public string Describtion {get; set;} }3. Using a custom attribute[Version (Name = "Hyddd", Date = "2009-07-20", describtion = "Hyddd ' s Class")]Public class MyCode{}4. Obtaining the associated information for a classpublic void xxx (){var add = (Versionattribute) attribute.getcustomattribute (typeof (MyCode), typeof (Versionattribute), false);// False is inherited, which specifies whether the indicated property can be inherited by a derived class and an overriding membervar Axx = (typeof (MyCode)). GetCustomAttributes (typeof (Versionattribute), false); //
Add. Name//Get the name attribute, scenario 1
foreach ( Versionattribute em in Axx) {
return EM. Name//Get the name attribute, Scenario 2
}
}

Reflection-optimization and assembly, etc. (the method (or property, field) that requires reflection calls is invoked in a delegate way, without using the Invoke method)

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.