C # class reflection instance

Source: Internet
Author: User
Create a class in a namespace Class1 And not in the same namespace Class1 ;
(Here, these classes are only used for called calls, which is of little significance. Reflection usage in Form class)

Class1 and form are in the same namespace
Using system;
Using system. Collections. Generic;
Using system. text;

Namespace fanshetest1
{
Class class1
{
Private string AB = "1 ";
Public class1 (string aa)
{
A = AA;
}
Public int AA (int x, int y)
{
Return X + Y + x + y;
}
Public String BB ()
{
Return "BB ";
}
Public static string CC ()
{
Return "cc ";
}
Public String AB
{
Get
{
Return AB;
}
Set
{
AB = value;
}
}
Public String;
}
}

The class1 and form forms are in different namespaces.
Using system;
Using system. Collections. Generic;
Using system. text;

Namespace fanshetest1
{
Class class1
{
Private string AB = "1 ";
Public class1 (string aa)
{
A = AA;
}
Public int AA (int x, int y)
{
Return X + Y + x + y;
}
Public String BB ()
{
Return "BB ";
}
Public static string CC ()
{
Return "cc ";
}
Public String AB
{
Get
{
Return AB;
}
Set
{
AB = value;
}
}
Public String;
}
}
The following describes how to use reflection to operate the above classes;

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. reflection;
Namespace fanshetest
{
Public partial class form1: Form
{
System. reflection. Assembly;
System. reflection. assembly B;
Public form1 ()
{
Initializecomponent ();
A = system. reflection. Assembly. loadfrom ("class1.dll ");
}

Private void button#click (Object sender, eventargs E)
{
Gouzaohanshu ();

}
// If no parameter is input, a type is returned;
Private void one ()
{
// Create another project and reference the classlibrary1.dll generated above in the project
System. type T = A. GetType ("class1.class1 ");
// Dynamically generate an instance of the classlibrary1.class class
Object theobj = system. activator. createinstance (t );
// Parameter information. getsum requires two string parameters.
System. Type [] paramtypes = new system. Type [2];
Paramtypes [0] = system. type. GetType ("system. String ");
Paramtypes [1] = system. type. GetType ("system. String ");
// System. reflection. methodinfo MI = T. getmethod ("AA", paramtypes );
System. reflection. methodinfo MI = T. getmethod ("BB ");
// Parameter value
Object [] parameters = new object [2];
Parameters [0] = 3;
Parameters [1] = 4;

Object returnvalue = mi. Invoke (theobj, null );
This. textbox1.text = returnvalue. tostring ();
}
// If no return value is returned, directly execute the function.
Private void none_void ()
{
// Create another project and reference the classlibrary1.dll generated above in the project
System. type T = A. GetType ("class1.class2 ");
// Dynamically generate an instance of the classlibrary1.class class
Object theobj = system. activator. createinstance (t );
// Parameter information. getsum requires two string parameters.
System. Type [] paramtypes = new system. Type [2];
// Call the method here. If there is a parameter, you only need to add "," and the type [] parameter of the parameter.
System. reflection. methodinfo MI = T. getmethod ("mes ");
Object returnvalue = mi. Invoke (theobj, null );

}
// No return value. Outgoing parameters receive the return value;
Private void two ()
{
Type T = A. GetType ("class1.class1 ");
Object theobj = activator. createinstance (t );
Type [] types = new type [2];
Types [0] = type. GetType ("system. int32 ");
Types [1] = type. GetType ("system. int32 ");
Object [] OBJ = new object [2];
OBJ [0] = 2;
OBJ [1] = 3;
Methodinfo MI = T. getmethod ("AA", types );
Object returnvalue = mi. Invoke (theobj, OBJ );
This. textbox1.text = returnvalue. tostring ();
}
// Obtain the reflected output value of the same namespace
Private void local ()
{
Type T = type. GetType ("fanshetest1.class1 ");
// Create an instance
Object theobj = activator. createinstance (t );
Type [] types = new type [2];
Types [0] = type. GetType ("system. int32 ");
Types [1] = type. GetType ("system. int32 ");
Object [] OBJ = new object [2];
OBJ [0] = 2;
OBJ [1] = 3;
Methodinfo MI = T. getmethod ("AA", types );
Object returnvalue = mi. Invoke (theobj, OBJ );
This. textbox1.text = returnvalue. tostring ();
}
// Obtain an attribute
Private void attribute ()
{
A = assembly. loadfrom ("class1.dll ");
Type T = A. GetType ("class1.class1 ");
T = type. GetType ("fanshetest1.class1 ");
Object theobj = activator. createinstance (t );
T. getproperty ("AB"). setvalue (theobj, "A", null );
This. textbox1.text = T. getproperty ("AB"). getvalue (theobj, null). tostring ();
}
// Obtain static functions;
Private void static _()
{
A = system. reflection. Assembly. loadfrom ("class1.dll ");
Type T = A. GetType ("class1.class1 ");
T = type. GetType ("fanshetest1.class1 ");
// Create an instance
Object theobj = activator. createinstance (t );
// Create an instance of a Method
Methodinfo MI = T. getmethod ("cc ");
Object returnvalue = mi. Invoke (theobj, null );
This. textbox1.text = returnvalue. tostring ();
}
// Obtain the variable;
Private void variable ()
{
A = assembly. loadfrom ("class1.dll ");
Type T = A. GetType ("class1.class1 ");
T = type. GetType ("fanshetest1.class1 ");
Object theobj = activator. createinstance (t );
Methodinfo MI = T. getmethod ("");
T. getfield ("A"). setvalue (theobj, "");
This. textbox1.text = T. getfield ("A"). getvalue (theobj). tostring ();
}
// Obtain the private variable reflection value;
Private void private _()
{

}
// Constructor
Private void gouzaohanshu ()
{
Type T = A. GetType ("class1.class1 ");
T = type. GetType ("fanshetest1.class1 ");
// Create the constructor type
Type [] structure_type = new type [1];
Structure_type [0] = type. GetType ("system. String ");
// Define the parameter passing type of the constructor
Object [] structure_obj = new object [1];
Structure_obj [0] = "AA ";
// Create an instance
Object theobj = activator. createinstance (T, structure_obj );
// Methodinfo structure_mi = T. getconstructor (structure_type );
// Methodinfo structure_mi = T. getmethod ("class1", structure_type );
// Structure_mi.invoke (theobj, structure_obj );

Methodinfo MI = T. getmethod ("");
// T. getfield ("A"). setvalue (theobj, "");
This. textbox1.text = T. getfield ("A"). getvalue (theobj). tostring ();
}

}
}

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.