获得“父对象"对象的反思 [英] Reflection to Get `Parent` Object

查看:39
本文介绍了获得“父对象"对象的反思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很确定这是不可能的,但我想我还是会问.

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

说我有

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

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

有没有可能通过反射使 B GetParentType 在运行时返回 typeof(A)?

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

我知道我可以在 A 上初始化 typeof(this) B 中时,我很好奇.

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

推荐答案

据我所知,答案是否定的,因为当您编写该文字时

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

var t = new B () ;

新的B实例没有有关将存储在何处的信息,因此该距离无法识别存储在var中的事实,因此示例中的实例不知道将其放置在A实例的属性中

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.

这篇关于获得“父对象"对象的反思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆