How do I cast an instance of an object and actually make it that type of object?
I have a class myClass1 that is the base class for myClass2 and myClass3. I want to use myClass1 for auditing, for auditing all I want is the data from myClass1. Because myClass2 and myClass3 inherit from myClass1 you can set an instance of myClass1 to an instance of myClass2 example:
myClass2 foo = new myClass2();
foo.prop1 = "some data";
foo.prop2 = "some More Data";
myClass1 bar = foo;
the problems come because I'm using a generic
public static IXPathNavigable SerializeGeneric<T>(T serializableObject)
{
String XmlizedString = "Error processing request";
XmlDocument XMLObject = new XmlDocument();
try
{
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(serializableObject.GetType());
to pass the class when I Serialize it and XmlSerializer throws an error because even though I have cast it as a myClass1 the underlying object is still a myClass2 you can see this by casting it an object and then checking the type and XmlSerializer get's confused because I'm telling it to make it a class1 be though it's own reflection it sees it as a myClass2
myClass2 foo = new myClass2();
foo.prop1 = "some data";
foo.prop2 = "some More Data";
myClass1 bar = foo;
object obj = bar;
string name = obj.GetType().Name;
the value of name is "myClass2" which makes sense seeing that the data in the memory is really a myClass2, underneath bar is just a pointer to a myClass2 object. Without creating a new instance and setting the values of that new instance to that object like
myClass1 bar = new myClass1(){prop1=foo.prop1, prop2=foo.prop2};
I really don't want to do it that way.