Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml;
  4. using System.Reflection;
  5.  
  6. namespace IoCContainer
  7. {
  8. public class IoCContainer
  9. {
  10. private static IDictionary<String,object> beans = new Dictionary<String, object>();
  11. private static IDictionary<Type, object> typeInstances = new Dictionary<Type, object>();
  12.  
  13. public IoCContainer(String xml)
  14. {
  15. XmlDocument doc = new XmlDocument();
  16. doc.LoadXml(xml);
  17. doc.Normalize();
  18. XmlNodeList nodeList = doc.GetElementsByTagName("bean");
  19. for (int nodeCnt = 0; nodeCnt < nodeList.Count; nodeCnt++)
  20. {
  21. String nodeClass = null;
  22. String nodeID = null;
  23. String attName = null;
  24. String attVal = null;
  25. String attRef = null;
  26.  
  27. XmlNode auxNode = nodeList[nodeCnt];
  28. XmlAttributeCollection auxNodeAttributes = auxNode.Attributes;
  29.  
  30. for(int attCnt = 0; attCnt < auxNodeAttributes.Count; attCnt++)
  31. {
  32. if(auxNodeAttributes[attCnt].Equals("id"))
  33. {
  34. nodeID = auxNodeAttributes[attCnt].ToString();
  35. }
  36. if(auxNodeAttributes[attCnt].Equals("class"))
  37. {
  38. nodeClass = auxNodeAttributes[attCnt].ToString();
  39. }
  40. }
  41.  
  42. Object obj = Activator.CreateInstance(Type.GetType(nodeClass));
  43.  
  44. for(int childNodeCnt = 0; childNodeCnt < auxNode.ChildNodes.Count; childNodeCnt++)
  45. {
  46. XmlNode auxChildNode = auxNode.ChildNodes[childNodeCnt];
  47. XmlAttributeCollection auxChildNodeAttributes = auxChildNode.Attributes;
  48. for(int attCnt = 0; attCnt < auxChildNodeAttributes.Count; attCnt++)
  49. {
  50. if (auxChildNodeAttributes[attCnt].Equals("value"))
  51. {
  52. attVal = auxChildNodeAttributes[attCnt].ToString();
  53. }
  54. if (auxChildNodeAttributes[attCnt].Equals("name"))
  55. {
  56. attName = auxChildNodeAttributes[attCnt].ToString();
  57. }
  58. if (auxChildNodeAttributes[attCnt].Equals("ref"))
  59. {
  60. attRef = auxChildNodeAttributes[attCnt].ToString();
  61. }
  62. }
  63. FieldInfo field = obj.GetType().GetRuntimeField(attName);
  64. field.SetValue(obj, attVal);
  65.  
  66. beans.Add(nodeID, obj);
  67. }
  68. }
  69.  
  70.  
  71. }
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement