private void ParseConfigurationFile() { XmlDocument doc = new XmlDocument(); doc.LoadXml(_fileContent); XmlNode root = doc.DocumentElement.SelectSingleNode("/objects"); if (root.IsNull()) { throw new ObjectsFactoryException("Unable to find root 'objects' node"); } foreach (XmlNode node in root.ChildNodes) { if (node.Name.ToLowerInvariant() != "object") { throw new ObjectsFactoryException("Found unknown node '{0}'".FormatWith(node.Name)); } string name = node.Attributes["name"].Value.ToString(); string assembly = node.Attributes["assembly"].Value.ToString(); string type = node.Attributes["type"].Value.ToString(); if (name.IsNullOrEmpty()) { throw new ObjectsFactoryException("Found node missing 'name' attribute"); } if (assembly.IsNullOrEmpty()) { throw new ObjectsFactoryException("Found node missing 'assembly' attribute"); } if (type.IsNullOrEmpty()) { throw new ObjectsFactoryException("Found node missing 'type' attribute"); } FactoryObject objDef = new FactoryObject { Assembly = assembly, Name = name, Type = type }; _objectList.Add(objDef); } }
public T GetObject <T>(string objName, params object[] args) where T : class { objName.AssertNotNull("objName"); FactoryObject factoryObj = _objectList.GetValue(objName); if (factoryObj.IsNull()) { throw new TypeLoadException("No configured objects found for name '{0}'".FormatWith(objName)); } Assembly assemblyObj = Assembly.Load(factoryObj.Assembly); Type typeObj = assemblyObj.GetType(factoryObj.Type); object obj = Activator.CreateInstance(typeObj, args); return(obj as T); }