Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflection to Get `Parent` Object [duplicate]

Tags:

c#

reflection

I'm pretty sure this isn't possible, but thought I'd ask anyways.

Say I have:

public class A
{
    public B BInstance { get; set; }
}

public class B
{
    public Type GetParentType()
    {
        //...
    }
}

Is there any possible way, via reflection, for B's GetParentType to return typeof(A) at runtime?

I know I could simply pass typeof(this) into B when I initialize it on A, I'm just curious.

like image 834
Jonesopolis Avatar asked Sep 02 '25 04:09

Jonesopolis


1 Answers

The answer is no, as far i know, because when you write that

var t = new B () ;

the new B instance has no information on where will be stored, so as that istance has no cognition on the fact that is stored in a var, the ones in your example do not know is put in a property of an A instance.

like image 162
Skary Avatar answered Sep 04 '25 18:09

Skary