1. There is a need for reflection in the project, careful study of the lower reflection, its efficiency is not high
Fortunately, Microsoft has provided the IL programming method, has implemented an efficient reflection class
Here are some common examples:
We reflect. An NETFramework class in the sessionstateutility and calls its private method deserialize
1 Public StaticSessionStateStoreData Deserialize (HttpContext context, stream stream)2 {3Dynamicmethodprivoder _sessionstateutility=NewDynamicmethodprivoder (typeof(sessionstateutility),NULL);5 return_sessionstateutility.methodinvoke ("Deserialize", context, Stream) asSessionStateStoreData;6}
Note: Because Dynamicmethodprivoder.deserialize is a static method, the second parameter of the Dynamicmethodprivoder construct is null
We reflect the private field in a Httpsessionstatecontainer object under HttpContext _sessionitems and get its value
1 Public StaticSessionStateStoreData Getsessionstoredate (HttpContext context)2 {3IHttpSessionState container =Sessionstateutility.gethttpsessionstatefromcontext (context);4Type _containertype =typeof(Httpsessionstatecontainer);5Dynamicmethodprivoder _containerprivoder =NewDynamicmethodprivoder (_containertype, container);6Isessionstateitemcollection Sessionitems = _containerprivoder.field ("_sessionitems") asisessionstateitemcollection;7Httpstaticobjectscollection StaticObjects =container. StaticObjects;8 return NewSessionStateStoreData (Sessionitems, StaticObjects, container. Timeout);9}
No more nonsense. Directly on the source: Http://files.cnblogs.com/dint/DynamicMethodPrivoder.rar
An efficient reflective class