Get parent class of null object (C # Reflection)

How to get the parent class of an object with a null value?

For instance...

ClassAcontains int? i, which is not set to any value when creating the class.

Then in some other place in the code I want to pass ias a parameter to some function. Using ias the only information, I want to understand what ClassA“belongs” i.

The reason for this is that it ClassAalso contains some other object, and I want to call this different value of the object the same function mentioned in the previous paragraph.

It may 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;
}
+3
source share
5

. A doSomething.

+6

class A () . .

, , , int int? doe, .

+3

, , , , . , , - nullable int, .

, , . - - , , , , .

+2

keyvaluepairs, int "s" ? , int , .

0
source

This is not possible because you pass "i", which is a member of class B. But class B does not contain a reference to an instance of class A. To get the value "s", an instance is required because of its non-static field.

0
source

Source: https://habr.com/ru/post/1736965/


All Articles