I want to build a dynamic type at run-time to compose multiple existing types in one dynamic type to simulate the following python scenario:
class A(object):
def Confirm(self):
print("A")
class B(A):
def Confirm(self):
super().Confirm()
print("B")
class C(A):
def Confirm(self):
print("C")
super().Confirm()
class D(A):
def Confirm(self):
print("D")
super().Confirm()
print("DD")
class E(B,C,D):
pass
I got the below outputs:
C
D
A
DD
B
so the call stack respects code order before and after super() call, executing all codes before super() call then super call itself finally the last part of code that comes after super() call. I want to simulate that behavior in c#.
public class A
{
public virtual void Confirm()
{
Console.Writeline("A");
}
}
public class B : A
{
public virtual void Confirm()
{
base.Confirm();
Console.Writeline("B");
}
}
public class C : A
{
public virtual void Confirm()
{
Console.Writeline("C");
base.Confirm();
}
}
public class D : A
{
public virtual void Confirm()
{
Console.Writeline("D");
base.Confirm();
Console.Writeline("DD");
}
}
public class E : A
{
public virtual void Confirm()
{
// BEFORE base
Console.Writeline("C"); // C.Confirm() without base, only code before base.Confirm()
Console.Writeline("D"); // D.Confirm() without base only code before base.Confirm()
// BASE
base.Confirm(); // base itself (A)
// AFTER base
Console.Writeline("DD"); // return to D.Confirm() only the code after base.Confirm()
Console.Writeline("B"); // B.Confirm() have only a code after base.Confirm() call
}
}
i think i can solve the problem with using of the System.Reflection.Emit
but i don't know if that possible.