. Net reflection technology Encapsulation

Source: Internet
Author: User

If someone asks you how to call a private method of a class or access a private member of a class, if you do not know the reflection, you will tell him that it is not possible. But with reflection, this is all possible. I sometimes think, since private is used to restrict access, why does reflection destroy this restriction? This problem may be explained through the side. If you want to maintain the class encapsulation, do not use reflection. reflection will damage the class encapsulation.

But in some cases, it will become quite useful. For example, you have a DLL, many of which are declared as internal. If you want to use these classes, you cannot modify them.Source codeThen you can use reflection. Recently, we have summarized it and encapsulated reflection. BecauseCodeRelatively simple, without comments.

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. reflection;

Namespace usefulutility
{
Public class invoker
{
Private Static bindingflags flags = bindingflags. Static | bindingflags. instance | bindingflags. nonpublic | bindingflags. Public | bindingflags. ignorecase;

Public static type GetType (Assembly ASM, string typefullname)
{
Return ASM. GetType (typefullname );
}

Public static type getinnertype (string hiddentypename, type outertype)
{
Module typemodule = outertype. module;
Return typemodule. GetType (hiddentypename );
}

Public static methodinfo getmethod (type, string funcname, type [] paramtypes)
{< br> If (paramtypes! = NULL)
return type. getmethod (funcname, flags, null, paramtypes, null);
else
return type. getmethod (funcname, flags);
}

Public static methodinfo getmethod (type, string funcname)
{
Return getmethod (type, funcname, null );
}

Public static object callmethod (Object OBJ, string funcname, Params object [] parameters)
{
Type type = obj. GetType ();
Methodinfo method = getmethod (type, funcname, gettypesfromobjects (parameters ));
Return method. Invoke (OBJ, parameters );
}

Public static object callstaticmethod (type, string funcname, Params object [] parameters)
{
Methodinfo method = getmethod (type, funcname, gettypesfromobjects (parameters ));
Return method. Invoke (null, parameters );
}

Public static object getproperty (Object OBJ, string propertyname)
{
Return getproperty (OBJ, propertyname, null );
}

Public static object getproperty (Object OBJ, string propertyname, Params object [] index)
{
Type type = obj. GetType ();
Return type. getproperty (propertyname, flags). getvalue (OBJ, index );
}

Public static object getstaticproperty (type, string propertyname)
{
Return getstaticproperty (type, propertyname, null );
}

Public static object getstaticproperty (type, string propertyname, Params object [] index)
{
Return type. getproperty (propertyname, flags). getvalue (null, index );
}

Public static void setproperty (Object OBJ, string propertyname, object value)
{
Setproperty (OBJ, propertyname, value, null );
}

Public static void setproperty (Object OBJ, string propertyname, object value, Params object [] index)
{
Type type = obj. GetType ();
Type. getproperty (propertyname, flags). setvalue (OBJ, value, index );
}

Public static void setstaticproperty (type, string propertyname, object value)
{
Setstaticproperty (type, propertyname, value, null );
}

Public static void setstaticproperty (type, string propertyname, object value, Params object [] index)
{
Type. getproperty (propertyname, flags). setvalue (null, value, index );
}

Public static object getfield (Object OBJ, string fieldname)
{
Type type = obj. GetType ();
Return type. getfield (fieldname, flags). getvalue (OBJ );
}

Public static object getsaticfield (type, string fieldname)
{
Return type. getfield (fieldname, flags). getvalue (null );
}

Public static void setfield (Object OBJ, string fieldname, object value)
{
Type type = obj. GetType ();
Type. getfield (fieldname, flags). setvalue (OBJ, value );
}

Public static void setstaticfield (type, string fieldname, object value)
{
Type. getfield (fieldname, flags). setvalue (null, value );
}

Private Static constructorinfo getconstructor (type, type [] paramtypes)
{
Return type. getconstructor (paramtypes );
}

Public static object createinstance (type, type [] paramtypes, Params object [] parameters)
{
Return getconstructor (type, paramtypes). Invoke (parameters );
}

Public static object createinstance (type, Params object [] parameters)
{
Return getconstructor (type, gettypesfromobjects (parameters). Invoke (parameters );
}

Private Static type [] gettypesfromobjects (object [] objs)
{< br> type [] types = new type [objs. length];
for (INT I = 0; I types [I] = objs [I]. getType ();
return types;
}

Public static void addeventhandler (Object OBJ, string eventname, methodinfo method, object methodowner)
{< br> eventinfo = obj. getType (). getevent (eventname, flags);
delegate eventdeleg = delegate. createdelegate (eventinfo. eventhandlertype, methodowner, method);
eventinfo. addeventhandler (OBJ, eventdeleg);
}

Public static void removeeventhandler (Object OBJ, string eventname, methodinfo method, object methodowner)
{
Eventinfo = obj. GetType (). getevent (eventname, flags );
Delegate eventdeleg = delegate. createdelegate (eventinfo. eventhandlertype, methodowner, method );
Eventinfo. removeeventhandler (OBJ, eventdeleg );
}
}
}

The following code tests some of the above methods, because there is only one fetion SDK at hand. DLL file, so he opened his knife to achieve simple login, login, send a test information to my mobile phone, for security omitted some information.

Using system;
Using system. Collections. Generic;
Using system. text;
Using usefulutility;
Using system. reflection;

Namespace consoleapplication1
{
Class Program
{
Static object SDK;
Static Assembly ASM;
[Stathread]
Static void main (string [] ARGs)
{
// Load Program Set
ASM = assembly. loadfrom ("fetion SDK. dll ");
// Obtain the fetion SDK type
Type type = invoker. GetType (ASM, "com. hetaoos. fetionsdk. fetionsdk ");
// Instantiate the SDK
SDK = invoker. createinstance (type );
// Obtain account management attributes
Object accountmanager = invoker. getproperty (SDK, "accountmanager ");
// Set the user name and password
Invoker. callmethod (accountmanager, "filluseridandpassword", new object [] {"mobile phone number", "Feixin password", false });
// Listen for events
Invoker. addeventhandler (SDK, "sdk_usersatuschange", invoker. getmethod (typeof (Program), "sdk_sdk_usersatuschange"), null );
// Call the logon Method
Invoker. callmethod (accountmanager, "login ");
}
Static void Hello ()
{
Console. writeline ("hello in hello ");
}

Static void sdk_sdk_usersatuschange (Object sender, eventargs E)
{

Console. writeline (invoker. getproperty (E, "newstatus "));
Console. writeline (invoker. getsaticfield (invoker. GetType (ASM, "imps. Client. useraccountstatus"), "Logon "));
// Here = is not easy to use. Equals is used. I don't know why
If (invoker. getproperty (E, "newstatus"). Equals (invoker. getsaticfield (invoker. GetType (ASM, "imps. Client. useraccountstatus"), "Logon ")))
{
Console. writeline ("hello ");
Object contactcontrol = invoker. getproperty (SDK, "contactcontrol ");
Object sendsms = invoker. getproperty (contactcontrol, "sendsms ");
Invoker. callmethod (sendsms, "sendsms", "Fly signal or mobile phone number to send information", "Hello, a test ");
}
}
}
}

Reprinted please indicate the source, the original address: http://www.cnblogs.com/dlutwy/archive/2009/04/28/1445352.html

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.