Consider these classes:
class ClassA
{
public int x;
}
class ClassB : ClassA
{
public int y;
}
then, somewhere in the code I instantiate a new ClassA:
var o = new ClassA();
var anotherRef = o;
This mean I'm getting a chunk of memory with size sizeof(ClassA) (say 4 bytes) starting in an address like 0x100.
Then somewhere I decide to change the type of o to ClassB and add the value of y.
So I want the o still address to 0x100 but with size of sizeof(ClassB) (say 8 bytes).
SomeMagicToChangeType(o, new ClassB());
// Now anotherRef should be a ClassB
In this case it will be good for anotherRef since it will reference to a correct instance.
Is it possible to do something like this in C#?
0x100it is not fixed. GC have all rights to move it over anytime.ClassA, at some place I understand that it's real type isClassBand that part is in another table. But as software did some work there are another references created. I want update the type ofoin a way that the other references don't break.interfaceorabstract classin both classes and use the interface without the concrete type.