Let’s say I have three classes A, B and C as below, A and B have property Pro1 and Pro2 with Type C:
class A
{
public C Pro1 {get; set; }
}
class B
{
public C Pro2 { get; set; }
}
public class C
{
public void Do()
{
//How to get Type of object to reference to current object C
//Example: Either type A or B
}
}
In the method Do of class C I want to get which current parent object to reference to current object C (via Pro1 and Pro2). In this sample either A or B, but in general, it could be very dynamic:
var a = new A() { Pro1 = new C() };
a.Pro1.Do(); //will get Type A in Do
var b = new B() { Pro2 = new C() };
b.Pro2.Do(); //with get Type B in Do
Which approach I can achieve this?
You could pass the “parent” object as a parameter to the C constructor: