I have the following classes:
public abstract class Animal
{
public Animal() { _myType = getAnimal(this.GetType().Name); }
private dynamic _myType;
public dynamic myType { get { return _myType; } }
}
public class Cat : Animal
{
public Cat() : base() { }
}
And its helper functions:
public static T CreateAnimal<T>(string animal)
{
Type type = Type.GetType(typeof(Form1).FullName + "+" + animal);
return (T)Activator.CreateInstance(type);
}
public static dynamic getAnimal(string name)
{
dynamic theAnimal = Activator.CreateInstance(MyAnimals); // Will default to 'Cat'
FieldInfo fi = MyAnimals.GetField(name);
int iEnum = (int)fi.GetValue(MyAnimals);
return Enum.ToObject(MyAnimals, iEnum);
}
It gets its 'myType' from the dynamically created enum 'MyAnimals':
public static Type MyAnimals;
public static void CreateAnimalEnum()
{
// Get the current application domain for the current thread.
AppDomain currentDomain = AppDomain.CurrentDomain;
// Create a dynamic assembly in the current application domain,
// and allow it to be executed and saved to disk.
AssemblyName aName = new AssemblyName("TempAssembly");
AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(
aName, AssemblyBuilderAccess.Run);
// Define a dynamic module in "TempAssembly" assembly. For a single-
// module assembly, the module has the same name as the assembly.
ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
// Define a public enumeration with an underlying type of Integer.
EnumBuilder eb = mb.DefineEnum("MyAnimalType", TypeAttributes.Public, typeof(int));
var types = new List<Type>();
int Count = 0;
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
try
{
types.AddRange(assembly.GetTypes().Where(x => x.IsSubclassOf(typeof(Animal))));
}
catch { }
foreach (var type in types)
eb.DefineLiteral(type.Name, Count++);
// Create the type and save the assembly.
MyAnimals = eb.CreateType();
}
So now when I create a cat, I cannot serialize it. "InvalidOperationException: There was an error generating the XML document." I have tried using DynamicObject, and I found a dynamic helper class (https://gist.github.com/martinnormark/2574972) but that doesnt help when I want to serialize a Cat object when its encapsulated in another class.
public static bool Save(Animal animal)
{
System.Xml.Serialization.XmlSerializer ListSer = new System.Xml.Serialization.XmlSerializer(typeof(Animal));
System.IO.StreamWriter mywriter = new System.IO.StreamWriter(@"test.txt", false);
ListSer.Serialize(mywriter, animal);
mywriter.Flush();
mywriter.Close();
return true;
}
public Form1()
{
InitializeComponent();
GetEDIDeviceTypesEnums();
Animal c = new Cat();
Save(c);
// This way fails too
dynamic cat = CreateAnimal<Animal>("Cat");
Save(cat);
}
What am I missing in order to serialize a Cat?