zvenny
November 19th, 2007, 04:02 AM
I'm experimenting with external (xml) type or class definitions. With the various Builder classes available in .Net I'm able to generate my types @ runtime, based on the info in an xml ...AssemblyBuilder ab;
ModuleBuilder mb;
TypeBuilder tb;
Dictionary<string,Type> dicTypes=new Dictionary<string,Type>();
public Form1()
{
InitializeComponent();
ab = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("MyRandomAssembly"), AssemblyBuilderAccess.Run);
mb = ab.DefineDynamicModule("MyRandomModule");
}
private void button_LoadXmlTypeDefs(object sender, EventArgs e)
{
dicTypes.Clear();
foreach (DefinedType dType in m_Xml.Types)
{
tb = mb.DefineType(dType.sName, TypeAttributes.Public);
foreach (DefinedField dField in dType.Fields)
{
Type tp;
switch (dField.sType)
{
case "U8" : tp=typeof(byte); break;
case "U16": tp=typeof(UInt16); break;
case "S16": tp=typeof(Int16); break;
default : break;
}
tb.DefineField(dField.sName, tp, FieldAttributes.Public);
}
dicTypes.Add(dType.sName, tb.CreateType());
}
}I've tested this with xml files containing 100s of definitions and with consecutive passes of button_LoadXmlTypeDefs memory usage keeps increasing as show by Task Manager. dicTypes.Clear doesn't seem to clean up the types of the previously 'loaded' Xml file !
So, any idea idea how to get rid of my runtime generated types ?
ModuleBuilder mb;
TypeBuilder tb;
Dictionary<string,Type> dicTypes=new Dictionary<string,Type>();
public Form1()
{
InitializeComponent();
ab = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("MyRandomAssembly"), AssemblyBuilderAccess.Run);
mb = ab.DefineDynamicModule("MyRandomModule");
}
private void button_LoadXmlTypeDefs(object sender, EventArgs e)
{
dicTypes.Clear();
foreach (DefinedType dType in m_Xml.Types)
{
tb = mb.DefineType(dType.sName, TypeAttributes.Public);
foreach (DefinedField dField in dType.Fields)
{
Type tp;
switch (dField.sType)
{
case "U8" : tp=typeof(byte); break;
case "U16": tp=typeof(UInt16); break;
case "S16": tp=typeof(Int16); break;
default : break;
}
tb.DefineField(dField.sName, tp, FieldAttributes.Public);
}
dicTypes.Add(dType.sName, tb.CreateType());
}
}I've tested this with xml files containing 100s of definitions and with consecutive passes of button_LoadXmlTypeDefs memory usage keeps increasing as show by Task Manager. dicTypes.Clear doesn't seem to clean up the types of the previously 'loaded' Xml file !
So, any idea idea how to get rid of my runtime generated types ?