C # example code reflected in practical applications

Source: Internet
Author: User

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

  1. <Deleetask>
  2. <Add key = "bizassembly" value = "psms. Biz"/>
  3. </Appsettings>

 

 

Step 2: Define a class for processing public assembly

 

[C-sharp]View plaincopy

  1. /// <Summary>
  2. /// Complete obtaining the proxy of the remote business logic object from the client
  3. /// </Summary>
  4. Public static class facadeservice
  5. {
  6. Static idictionary <string, type> serviceclasscattionary; // defines a key-value pair interface object
  7. Static facadeservice ()
  8. {
  9. Serviceclasscatalog = new dictionary <string, type> ();
  10. Assembly = assembly. Load (New assemblyname (configurationmanager. etettings ["bizassembly"]); // starts to load Assembly objects
  11. Type [] types = assembly. getexportedtypes (); // gets the type set of all objects in the Assembly.
  12. Type basetype = typeof (marshalbyrefobject );
  13. Foreach (type in types)
  14. {
  15. If (basetype. isassignablefrom (type ))
  16. {
  17. Type [] interfaces = type. getinterfaces ();
  18. // Register the final derived interface type of the interface type, that is, the top interface
  19. If (interfaces. length> 0)
  20. {
  21. Serviceclasscatalog. Add (interfaces [0]. fullname, type );
  22. }
  23. }
  24. }
  25. }
  26. /// <Summary>
  27. /// Return the type object instance remote proxy that implements the interface based on the Interface Type of the incoming business logic class
  28. /// </Summary>
  29. /// <Typeparam name = "ifacade"> specific business logic interface type </typeparam>
  30. /// <Returns> remote proxy for the Type object instance that implements this interface </returns>
  31. Public static ifacade getfacade <ifacade> ()
  32. {
  33. String typename = typeof (ifacade). fullname;
  34. If (serviceclasscatalog. containskey (typename ))
  35. {
  36. Object realproxy = activator. createinstance (serviceclasscatalog [typename]);
  37. Return (ifacade) realproxy;
  38. }
  39. Else
  40. {
  41. Throw new exception ("does not include the service type defined by the interface. ");
  42. }
  43. }
  44. }

 

 

Step 3: implement the call in the program code

 

[C-sharp]View plaincopy

  1. Public partial class mytest: system. Web. UI. Page
  2. {
  3. // Create an instance object for the (tested) interface in the background code
  4. Static iuserinfofacade userinfofacade = facadeservice. getfacade <iuserinfofacade> ();
  5. // Other function implementation code
  6. //......
  7. //......
  8. Private void Method1 ()
  9. {
  10. // Specific call
  11. List <userinfo> lstuserinfo = userinfofacade. getuserinfolist (unitcode, 0, 0 );
  12. // Other function implementation code
  13. //......
  14. //......
  15. }
  16. }

 

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.