Reflection implementation method call (1): execution mechanism

Source: Internet
Author: User

1. Reflection Overview
2. Use reflection
3. Reflect the execution mechanism of calling methods

 

1. Reflection Overview

The Assembly contains modules, while the module contains types and types including members. Reflection provides encapsulated assembly, module, and type objects. 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. Then, you can call methods of the type or access its fields and attributes. (From msdn)

 

2. Use reflection

2.1 reflection Object Model (FROM Essential. Net)

 

2.2 reflection type hierarchy (FROM Essential. Net)

 

2.3 creation methods and functions of each object

Object Create this object Function (from msdn)
Assembly Assembly. Load
Assembly. LoadFile
Assembly. LoadFrom
Use Define and load the Assembly, load the modules listed in the assembly list, and find the type from this Assembly and create instances of this type.
Assembly. GetModule
Assembly. GetModules

Use The following information is found: including the assembly of the module and the class in the module. You can also obtain all global methods defined on the module or other specific non-Global methods.

Type. GetConstructor
Type. GetConstructors
Type. GetConstructorImpl

Use The following information is found: name, parameter, and access modifier of the constructor (for examplePublicOrPrivate) And implementation details (suchAbstractOrVirtual. Use Of Or Method to call a specific constructor.

Type. GetMethod
Type. GetMethods
Type. GetMethodImpl
Use The following information is found: method name, return type, parameter, and access modifier (suchPublicOrPrivate) And implementation details (suchAbstractOrVirtual.
Type. GetField
Type. GetFields
Use The following information is found: field name and access modifier (suchPublicOrPrivate) And implementation details (suchStatic), And obtain or set the field value.
Type. GetEvent
Type. GetEvents
Use The following information is found: event name, event handler data type, custom attribute, declaration type, and reflection type, and add or remove event handler.
Type. GetProperty
Type. GetProperties
Type. GetPropertyImpl
Use The following information is found: attribute name, data type, declaration type, reflection type, read-only or writable state, and the attribute value is obtained or set.
MethodBase. GetParameters Use The following information is found: Parameter Name, data type, whether the parameter is an input parameter or an output parameter, and the position of the parameter in the method signature.
    Use To learn about custom attributes. Use You do not have to create Attribute instances to check them.

 

2.4 reflection call instance

namespace NameSpace
{
    internal class Reflect
    {
        private int Add(int arg0, int arg1, int arg2)
        {
            return arg0 + arg1 + arg2;
        }
    }
 
    class Program
    {
        internal static int CallAdd()
        {
// Specify TypeName to create an object instance
            object target = Assembly.GetExecutingAssembly().CreateInstance("NameSpace.Reflect", true);
 
// Obtain Method
            Type type = target.GetType();
            MethodInfo methodInfo = type.GetMethod("Add", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
 
            if (methodInfo == null)
                return 0;
 
            object[] args = new object[] { 10, 20, 30 };
Object result = methodInfo. Invoke (target, args); // call
            return Convert.ToInt32(result);
        }
    }
}

 

3. Reflect the execution mechanism of calling methods

MethodInfo. Invoke uses the provided object reference and parameters to call the underlying method. The execution process is as follows (FROM Essential. Net):
(1). Build a stack frame;
(2). Copy the parameter value to the stack;
(3). call the target method (call Command of the IA-32 processor );
(4) After the method is executed, Invoke identifies the parameters passed by reference and copies them back to the parameter value array;
(5) If the method has a return value, the value is used as the return value of Invoke.

The execution process of the sample code in 2.4 is shown as follows:

 

Supplement:
1. For details about stack frames, refer to Reversing: reverse engineering decryption stack frames.
2. Shows the fastcall call Convention. The fastcall call Convention usually uses the ECX register and the EDX register to store the first and second parameters respectively. For more information about the call conventions, see Reversing: reverse engineering secrets.

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.