(C #) Using reflex dynamic call classes

xiaoxiao2021-03-06  37

(C #) Using reflex dynamic call classes

Using reflected dynamic call classes, a method of the Type class is required: InvokeMember. The statement on this method is as follows (extracted in MSDN):

Public Object InvokeMember

String Name,

Bindingflags Invokeattr,

Binder Binder,

Object Target,

Object [] ARGS

);

parameter

Name

String, which contains the name of constructor, method, attribute, or field member to be called.

- or -

Empty string ("") indicates calling the default member.

INVOKEATTR

One bit shields, composed of one or more BindingFlags that specifies a search execution method. Access can be one of BindingFlags, such as Public, Nonpublic, Private, InvokeMethod, and GetField, etc. You don't need to specify the type of lookup. BindingFlags.Public | bindingflags.instance will be applied if the type of lookup is omitted.

Binder

A Binder object defines a set of properties and enables binding, and binding may involve selecting an overloaded method, forced parameter type, and a member of the reflection.

- or -

If you are an empty reference (Nothing in Visual Basic), you will use the defaultbinder.

Target

To measure the Object of the designated member on it.

argg

An array containing the parameters passing to the member to be called.

return value

Indicates the Object of the return value called member.

Remarks:

The following BindingFlags filter flags can be used to define members contained in the search:

To get the return value, you must specify bindingflags.instance or bindingflags.static.

Specify bindingflags.publics can include public members in search.

Specify bindingflags.nonpublics can include non-public members (ie private members and protected members) in search.

Specifies BindingFlags.Flattenhirarchy to include static members on hierarchies.

The following bindingflags modifiers can be used to change the search for execution:

BindingFlags.Ignorecase, indicating that the big write of Name is ignored.

BindingFlags.DeclaredOnly, only the members of the declaration on Type, instead of being searched as a member of the simple inheritance.

You can use the following BindingFlags call logo to indicate the action you want to take to members:

CreateInstance indicates call constructor. Ignore the Name. Invalid for other call logos.

InvokeMethod, indicating calling method without calling constructor or type initial value setting item. Invalid for SetField or SetProperty.

GetField, indicates that the field value is obtained. Impact on SetField.

SetField, indicates setting field values. Invalid for GetField.

GetProperty indicates the acquisition property. Invalid for SetProperty.

SetProperty represents setting properties. Invalid for GetProperty.

The following is a simple application of this method (I have always thought that you have to make the exemplary, the more simple and more intuitive.)

Using system;

Using system.reflection;

Namespace consoleApplication9

{

Class love {

Public int field1;

Private string _name;

Public love ()

{

}

Public String Name

{

get

{

Return_name;

}

set

{

_name = value;

}

}

Public int GetInt (INT A)

{

Return A;

}

Public Void Display (String STR)

{

Console.writeLine (STR);

}

}

///

/// Class1 summary description.

///

Class class1

{

///

/// The main entry point for the application.

///

[Stathread]

Static void main (string [] args)

{

//

// Todo: Add code here to start the application

//

Love love = new love ();

TYPE TYPE = love.gettype ();

Object obj = type.invokemember (NULL,

BindingFlags.DeclaredOnly |

BindingFlags.Public | BindingFlags.nonpublic |

Bindingflags.instance | BindingFlags.createInstance, Null, Null, ARGS;

// Call the method without returning value

Type.Invokemember ("Display", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.instance, Null, Obj, New Object [] {"Aldfjdlf"});

// Call the method of returning value

INT i = (int) TYPE.INVOKEMEMBER ("GetInt", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.instance, Null, Obj, New Object [] {1});

Console.writeLine (i);

/ / Set the attribute value

Type.invokemember ("name", bindingflags.setproperty, null, obj, new string [] {"ABC"});

// Get attribute values

String str = (string) Type.Invokemember ("name", bindingflags.getProperty, NULL, OBJ, NULL);

Console.writeLine (STR);

// Set field value

Type.Invokemember ("Field1", BindingFlags.Setfield, Null, Obj, New Object [] {444});

// Get field value

INT f = (int) Type.Invokemember ("Field1", BindingFlags.Getfield, NULL, OBJ, NULL);

Console.writeLine (f);

Console.readline ();

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-118576.html

New Post(0)