C # reflects on configuration files

Source: Internet
Author: User

Since reflection is used in the project, it is prepared to write each class library first in the configuration file, then read the configuration file, and then reflect the class library that is configured in the configuration file.

The benefit of this is that each class library remains independent, where a problem with one class library does not affect other class libraries, as long as the individual DLLs are updated when the project is updated.

1. First look at how the configuration file is configured

The custom node processors is created in the configuration file above, and then the method node is corresponding below the processors node.

Two methods were used in the above project, so two nodes were created. Type represents the assembly name, and method represents the name.

2. See how to use the configuration file

[HTML]View Plaincopy
  1. private void Button1_Click (object sender, EventArgs e)
  2. {
  3. Returns the node name and metadata information in the configuration file
  4. IDictionary<string,methodinfo> dic=loadappconfig (application.startuppath + "\ \  App. Config ");
  5. Test GetInfo method
  6. var mi = dic["GetInfo"];
  7. var obj = activator.createinstance (mi.  DeclaringType);
  8. Object[] para={"Hello"};//if the length of the array does not match the number of arguments, the exception will be reported
  9. Return method Result Value
  10. Object result = mi.  Invoke (obj, para);
  11. MessageBox.Show (result. ToString ());
  12. }
  13. <summary>
  14. Reading configuration Files
  15. </Summary>
  16. <param name="path"></param>
  17. <returns></returns>
  18. Private Dictionary<string, MethodInfo> Loadappconfig (string filename)
  19. {
  20. var processors = new Dictionary<string, MethodInfo> ();
  21. var xml = new XmlDocument ();
  22. Xml. Load (filename);
  23. foreach (XmlNode node in XML. SelectNodes ("configuration/processors/*"))
  24. {
  25. if (node. NodeType! = xmlnodetype.element)
  26. Continue
  27. var method = node.  Name;
  28. Try
  29. {
  30. String TypeName = node. attributes["type"]. value;//assembly name
  31. String MethodName = node. Attributes["Method"]. value;//Method Name
  32. var t = Type.GetType (typeName);
  33. var m = T.getmethod (methodName);
  34. processors. Add (MethodName, M);
  35. }
  36. catch (Exception e)
  37. {
  38. }
  39. }
  40. return processors;
  41. }


Demo Download: http://download.csdn.net/detail/zx13525079024/4734769

http://blog.csdn.net/zx13525079024/article/details/8149517

C # reflects on configuration files

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.