c# - 获取空对象的父类(C# 反射)

标签 c# object null gettype

如何获取值为 null 的对象的父类?

例如……

ClassA 包含 int? i 在创建类时未设置任何值。

然后在代码的其他地方,我想将 i 作为参数传递给某个函数。使用 i 作为唯一的信息,我希望能够弄清楚 ClassA“拥有”i

这是因为 ClassA 还包含一些其他对象,我想从上一段中提到的同一函数调用这个其他对象的值。

也可以是:

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;
}

最佳答案

你不能。将 A 的实例传递给 doSomething

关于c# - 获取空对象的父类(C# 反射),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2446047/

相关文章:

c# - 如何知道是否应用了 OrderBy 进行查询?

c# - 如何从 HttpresponseMessage Asp.net Web Api 返回不记名 token

c# - 主线程是前台线程还是后台线程?

c++ - 是否存在堆分配对象的永不为空的唯一所有者?

java - 为什么 Boolean 总是给出 null

c# - 在 Windows 8 中检查互联网连接(可用性)

javascript - 我可以在JS中获取一个对象,但不能获取它的属性

html - 如何在Grails中显示最终的显示页面范围?

javascript - 使用 var 或不使用 var 定义对象

java - 为什么我的 Spring @Autowired 字段为空?