(C #) dynamic invocation of class members with reflection

Source: Internet
Author: User
Tags constructor contains reflection
Dynamic (C #) dynamic invocation of class members with reflection



Invoking a class member dynamically using reflection requires a method of the type class: InvokeMember. The declaration of the method is as follows (excerpt from MSDN):

public Object InvokeMember (

String name,

BindingFlags invokeattr,

Binder Binder,

Object Target,

Object[] Args

);

Parameters

Name

String that contains the name of the constructor, method, property, or field member to invoke.

Or

An empty string ("") that represents calling the default member.

Invokeattr

A bitmask consisting of one or more BindingFlags that specify how the search executes. Access can be one of the BindingFlags, such as public, nonpublic, Private, InvokeMethod, and GetField. You do not need to specify a lookup type. If the lookup type is omitted, the BindingFlags.Public is applied | BindingFlags.Instance.

Binder

A Binder object that defines a set of properties and enables binding, which may involve the selection of overloaded methods, coercion of parameter types, and invocation of members by reflection.

Or

If it is a null reference (Nothing in Visual Basic), use DefaultBinder.

Target

The Object on which to invoke the specified member.

Args

An array that contains the arguments that are passed to the member to be invoked.



return value

Object that represents the return value of the invoked member.



Note:

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



In order to get the return value, you must specify either BindingFlags.Instance or BindingFlags.Static.

Specifies that BindingFlags.Public can include public members in the search.

Specifies that BindingFlags.NonPublic can include non-public members (that is, private and protected members) in the search.

Specifies that bindingflags.flattenhierarchy can contain static members on the hierarchy.

The following BindingFlags modifier flags can be used to change how the search is executed:



Bindingflags.ignorecase, which indicates that the case of name is ignored.

BindingFlags.DeclaredOnly, only the members declared on the Type are searched, not the members that are simply inherited.

You can use the following BindingFlags call flags to represent actions to be taken on a member:



CreateInstance, which represents calling the constructor. Ignore name. Invalid for other invocation flags.

InvokeMethod, which means calling a method without invoking a constructor or type initializer. Invalid for SetField or SetProperty.

GetField, representing the Get field value. Invalid for SetField.

SetField, which indicates setting the field value. Invalid for GetField.

GetProperty, representing the Get property. Invalid for SetProperty.

SetProperty represents setting properties. Invalid for GetProperty.



The following examples of this method for a simple application (I always thought, to let the example to make it easier to understand the meaning and role of the text, the more simple examples of writing the more intuitive the better.) )

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);

}

}



<summary>

Summary description of the CLASS1.

</summary>

Class Class1

{

<summary>

The main entry point for the application.

</summary>

[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);





To invoke a method that does not return a value

Type. InvokeMember ("Display", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, NULL, obj, new object[]{"Aldfjdlf"});



Calling methods that have return values

int i = (int) type. InvokeMember ("GetInt", BindingFlags.InvokeMethod | BindingFlags.Public | Bindingflags.instance,null,obj,new object[]{1});

Console.WriteLine (i);



Setting property values

Type. InvokeMember ("Name", Bindingflags.setproperty,null,obj,new string[]{"abc"});

Get property value

String str= (String) type. InvokeMember ("Name", bindingflags.getproperty,null,obj,null);

Console.WriteLine (str);



Set field values

Type. InvokeMember ("Field1", Bindingflags.setfield,null,obj,new object[]{444});



Get field values

int f= (int) type. InvokeMember ("Field1", bindingflags.getfield,null,obj,null);

Console.WriteLine (f);

Console.ReadLine ();

}

}

}






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.