Reflection provides encapsulated assembly, module, and type objects (type ). You can use reflection to dynamically create instances of the type, bind the type to an existing object, or obtain the type from the existing object and call its method or access its fields and properties. If attributes are used in the code, you can use reflection to access them.
The following example shows how to use reflection in a project.
There are three steps:
Step 1: configure the following code in Web. config (to dynamically modify the DLL to be analyzed)
[C-sharp]View plaincopy
- <Deleetask>
- <Add key = "bizassembly" value = "psms. Biz"/>
- </Appsettings>
Step 2: Define a class for processing public assembly
[C-sharp]View plaincopy
- /// <Summary>
- /// Complete obtaining the proxy of the remote business logic object from the client
- /// </Summary>
- Public static class facadeservice
- {
- Static idictionary <string, type> serviceclasscattionary; // defines a key-value pair interface object
- Static facadeservice ()
- {
- Serviceclasscatalog = new dictionary <string, type> ();
- Assembly = assembly. Load (New assemblyname (configurationmanager. etettings ["bizassembly"]); // starts to load Assembly objects
- Type [] types = assembly. getexportedtypes (); // gets the type set of all objects in the Assembly.
- Type basetype = typeof (marshalbyrefobject );
- Foreach (type in types)
- {
- If (basetype. isassignablefrom (type ))
- {
- Type [] interfaces = type. getinterfaces ();
- // Register the final derived interface type of the interface type, that is, the top interface
- If (interfaces. length> 0)
- {
- Serviceclasscatalog. Add (interfaces [0]. fullname, type );
- }
- }
- }
- }
- /// <Summary>
- /// Return the type object instance remote proxy that implements the interface based on the Interface Type of the incoming business logic class
- /// </Summary>
- /// <Typeparam name = "ifacade"> specific business logic interface type </typeparam>
- /// <Returns> remote proxy for the Type object instance that implements this interface </returns>
- Public static ifacade getfacade <ifacade> ()
- {
- String typename = typeof (ifacade). fullname;
- If (serviceclasscatalog. containskey (typename ))
- {
- Object realproxy = activator. createinstance (serviceclasscatalog [typename]);
- Return (ifacade) realproxy;
- }
- Else
- {
- Throw new exception ("does not include the service type defined by the interface. ");
- }
- }
- }
Step 3: implement the call in the program code
[C-sharp]View plaincopy
- Public partial class mytest: system. Web. UI. Page
- {
- // Create an instance object for the (tested) interface in the background code
- Static iuserinfofacade userinfofacade = facadeservice. getfacade <iuserinfofacade> ();
- // Other function implementation code
- //......
- //......
- Private void Method1 ()
- {
- // Specific call
- List <userinfo> lstuserinfo = userinfofacade. getuserinfolist (unitcode, 0, 0 );
- // Other function implementation code
- //......
- //......
- }
- }