(Continuing my previous post)
The dynamically supported classes must be configured. So we need to read them from the config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DynamicClassFactoryItemList" type="DynamicClassFactory.DynamicClassFactorySectionHandler, DynamicClassFactory" />
</configSections>
<appSettings></appSettings>
<DynamicClassFactoryItemList>
<DynamicClassFactoryItem type="DynamicClassFactory.DynamicClassFactoryItem, DynamicClassFactory">
<Key>0</Key>
<ClassName>MyNamespace.MyClass</ClassName>
<AssemblyName>c:\MyPath\MyClassAssembly.dll</AssemblyName>
<ReferenceLibs>
<ReferenceLib>c:\MyReferences</ReferenceLib>
</ReferenceLibs>
</DynamicClassFactoryItem>
</DynamicClassFactoryItemList>
</configuration>
/// <summary>
/// Summary description for DynamicClassFactoryItem.
/// </summary>
[Serializable]
public class DynamicClassFactoryItem
{
private int key;
private string className;
private string assemblyName;
private ArrayList referenceLibs;
public DynamicClassFactoryItem()
{
this.referenceLibs = new ArrayList();
}
public DynamicClassFactoryItem(DynamicClassFactoryItem other)
{
this.key = other.key;
this.className = other.className;
this.assemblyName = other.assemblyName;
this.referenceLibs = other.referenceLibs;
}
public DynamicClassFactoryItem(int key, string className, string assemblyName)
{
this.key = key;
this.className = className;
this.assemblyName = assemblyName;
this.referenceLibs = new ArrayList();
}
public DynamicClassFactoryItem(int key, string className, string assemblyName, ArrayList referenceLibs)
{
this.key = key;
this.className = className;
this.assemblyName = assemblyName;
this.referenceLibs = referenceLibs;
}
public DynamicClassFactoryItem(int key, string className, string assemblyName, params object[] referenceLibs)
{
this.key = key;
this.className = className;
this.assemblyName = assemblyName;
if (referenceLibs.Length > 0)
{
this.referenceLibs = new ArrayList(referenceLibs);
}
}
public int Key
{
get { return key; }
set { key = value; }
}
public string ClassName
{
get { return className; }
set { className= value; }
}
public string AssemblyName
{
get { return assemblyName; }
set { assemblyName = value; }
}
public ArrayList ReferenceLibs
{
get { return referenceLibs; }
set { referenceLibs = value; }
}
}
/// <summary>
/// Reads the portion of a configuration file that holds DynamicClassFactory information
/// </summary>
public class DynamicClassFactorySectionHandler : IConfigurationSectionHandler
{
public DynamicClassFactorySectionHandler() : base()
{
}
public object Create(object parent, object configContext, XmlNode section)
{
DynamicClassFactoryItem[] items = new DynamicClassFactoryItem[0];
try
{
XmlNodeList nodes = section.SelectNodes("DynamicClassFactoryItem");
items = new DynamicClassFactoryItem[nodes.Count];
foreach (XmlNode node in nodes)
{
XPathNavigator xNav=node.CreateNavigator();
string typeOfObject=(string) xNav.Evaluate("string(@type)");
Type t=Type.GetType(typeOfObject);
XmlSerializer ser=new XmlSerializer(t);
XmlNodeReader xNodeReader=new XmlNodeReader(node);
DynamicClassFactoryItem current = (DynamicClassFactoryItem)ser.Deserialize(xNodeReader);
XmlNodeList refLibs = node.SelectNodes(@"ReferenceLibs/ReferenceLib");
foreach (XmlNode refLib in refLibs)
{
current.ReferenceLibs.Add(refLib.InnerText);
}
items[current.Key] = current;
}
}
catch (Exception e)
{
Debug.WriteLine(string.Format("An exception occurred in DynamicClassFactorySectionHandler.Create: {0}", e.Message));
throw;
}
return items;
}
}
No comments:
Post a Comment