Get the parent class of a null object (C# Reflection)

Posted by Nick on Stack Overflow See other posts from Stack Overflow or by Nick
Published on 2010-03-15T09:21:50Z Indexed on 2010/03/15 9:29 UTC
Read the original article Hit count: 181

Filed under:
|
|
|

How would I get the parent class of an object that has a value of null?

For example...

'Class A' contains 'int? i' which is not set to any value when the class is created.

Then in some other place in the code I want to pass in 'i' as a parameter to some function. Using 'i' as the only info, I want to be able to figure out that 'Class A' "owns" 'i'.

The reason for this is because 'Class A' also contains some other object, and I want to call this other object's value from that same function mentioned in the above paragraph.

Could also be:

public class A
{
    public class B
    {
        public int? i;
        public int? j;
    }

    B classBInstance = new B();
    public string s;
}

{
    ...
    A someClassAInstance = new A();
    ...
    doSomething(someClassAInstance.classBInstance.i);
    ...
}

public static bool doSomething(object theObject)
{
    string s = /* SOMETHING on theObject to get to "s" from Class A */;
    int someValue = (int)theObject;
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about gettype