Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Xml;
- using System.Reflection;
- namespace IoCContainer
- {
- public class IoCContainer
- {
- private static IDictionary<String,object> beans = new Dictionary<String, object>();
- private static IDictionary<Type, object> typeInstances = new Dictionary<Type, object>();
- public IoCContainer(String xml)
- {
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(xml);
- doc.Normalize();
- XmlNodeList nodeList = doc.GetElementsByTagName("bean");
- for (int nodeCnt = 0; nodeCnt < nodeList.Count; nodeCnt++)
- {
- String nodeClass = null;
- String nodeID = null;
- String attName = null;
- String attVal = null;
- String attRef = null;
- XmlNode auxNode = nodeList[nodeCnt];
- XmlAttributeCollection auxNodeAttributes = auxNode.Attributes;
- for(int attCnt = 0; attCnt < auxNodeAttributes.Count; attCnt++)
- {
- if(auxNodeAttributes[attCnt].Equals("id"))
- {
- nodeID = auxNodeAttributes[attCnt].ToString();
- }
- if(auxNodeAttributes[attCnt].Equals("class"))
- {
- nodeClass = auxNodeAttributes[attCnt].ToString();
- }
- }
- Object obj = Activator.CreateInstance(Type.GetType(nodeClass));
- for(int childNodeCnt = 0; childNodeCnt < auxNode.ChildNodes.Count; childNodeCnt++)
- {
- XmlNode auxChildNode = auxNode.ChildNodes[childNodeCnt];
- XmlAttributeCollection auxChildNodeAttributes = auxChildNode.Attributes;
- for(int attCnt = 0; attCnt < auxChildNodeAttributes.Count; attCnt++)
- {
- if (auxChildNodeAttributes[attCnt].Equals("value"))
- {
- attVal = auxChildNodeAttributes[attCnt].ToString();
- }
- if (auxChildNodeAttributes[attCnt].Equals("name"))
- {
- attName = auxChildNodeAttributes[attCnt].ToString();
- }
- if (auxChildNodeAttributes[attCnt].Equals("ref"))
- {
- attRef = auxChildNodeAttributes[attCnt].ToString();
- }
- }
- FieldInfo field = obj.GetType().GetRuntimeField(attName);
- field.SetValue(obj, attVal);
- beans.Add(nodeID, obj);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement