I try to get the parent Type of a instance. How can I do ?
Example :
public class a
{
public b { get; set; }
}
public class b
{
}
var a = new a();
a.b = new b();
var parentType = a.b.??GetParentInstanceType()??
You can't.
You'd need to add a property to the child manually to keep track of the parent:
Here is one way:
public class A
{
public B<A> Child { get; set; }
}
public class B<T>
{
public T Parent { get; set; }
}
A a = new A();
a.Child = new B<A>();
a.Child.Parent = a;
Type parentType = a.Child.Parent.GetType();
Of course the problem here is that nothing stops you from forgetting to set Parent or setting the wrong Parent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With