C#:反射:获取创建的类对象

发布于 2024-09-15 04:01:05 字数 1078 浏览 8 评论 0原文

我正在研究如何获取创建另一个对象的对象(或对象类型)。例如:

public class RootClass
{
    public class A
    {
        public void MethodFromA()
        {

        }
    }

    public class B
    {
        public A A_object = new A();
        public void MethodFromB() { }
    }

    B BObject = new B();
    A rootAObject = new A();

    public void DoMethod(A anA_object)
    {

    }

    main()
    {
        /* Somehow through reflection
         * get the instance of BObject 
         * of type B from only the info
         * given in anA_object or at
         * the very least just know that
         * anA_object was created in
         * class B and not root. */
        DoMethod(BObject.A_object);

        /* Same as above except know that
         * this A object came from root 
         * and is not from B class */
        DoMethod(rootAObject);
    }
}

附加信息: 这只是一个快速代码,用于模拟我拥有的大型项目的一部分。问题是我有一个自定义类,它在其他各种类的许多地方实例化。这个自定义类有一个函数,应该能够调用其中的任何函数或实例化它的类中的任何函数。非常通用的处理,但需要。基本上我需要“.”的反义词。因此,对于 objectA.objectB,我只需要通过将 objectB 传递给某个函数来找到 objectA。

谢谢!

I am looking on how to get the object (or object type) that another object is created in. For example:

public class RootClass
{
    public class A
    {
        public void MethodFromA()
        {

        }
    }

    public class B
    {
        public A A_object = new A();
        public void MethodFromB() { }
    }

    B BObject = new B();
    A rootAObject = new A();

    public void DoMethod(A anA_object)
    {

    }

    main()
    {
        /* Somehow through reflection
         * get the instance of BObject 
         * of type B from only the info
         * given in anA_object or at
         * the very least just know that
         * anA_object was created in
         * class B and not root. */
        DoMethod(BObject.A_object);

        /* Same as above except know that
         * this A object came from root 
         * and is not from B class */
        DoMethod(rootAObject);
    }
}

Additional Information:
This was just a quick code to simulate part of a large project I have. The problem is I have a custom class that is instantiated many many places in various other classes. This custom class has a function that should be able to call any function in it or any function in the class that instantiated it. Very generic processing, but needed. Basically I need the inverse of ".". So with objectA.objectB, I need to find objectA only from passing in objectB to some function.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

〆一缕阳光ご 2024-09-22 04:01:05

不 - 此信息不会存储在任何地方。请注意,即使是这样,它也很容易过时。例如:

// Code as before
BObject.A_object = rootAObject;
rootAObject = null;
DoMethod(BObject.A_object);

现在应该显示什么? BObject.A_object 的当前值已创建rootAObject,但 rootAObject 的当前值为 null。如果您希望将 BObject 显示为“所有者”,那么您根本就没有真正谈论创建...此时,您需要处理一个对象有多个引用的可能性。

请向我们提供更多有关大局的信息:您真正想解决什么问题?

No - this information isn't stored anywhere. Note that even if it were, it could easily become out of date, effectively. For example:

// Code as before
BObject.A_object = rootAObject;
rootAObject = null;
DoMethod(BObject.A_object);

What should that now show? The current value of BObject.A_object was created as rootAObject, but the current value of rootAObject is null. If you'd want that to show BObject as the "owner" then you're not really talking about creation at all... and at that point, you need to deal with the possibility that an object has multiple references to it.

Please give us more information about the bigger picture: what problem are you really trying to solve?

零度° 2024-09-22 04:01:05

我相信您正在寻找的是在您感兴趣的 System.Type 实例上定义的属性 DeclaringType。请参阅 DeclaringType 文档

I believe what you're looking for is the property DeclaringType, defined on the System.Type instance that you're interested in. See DeclaringType documentation.

我们只是彼此的过ke 2024-09-22 04:01:05

DeclaringType 只会告诉您代码模型的封闭类型,但您要做的是识别对象创建点。

除了阅读单独的 MethodBody IL 之外,没有其他简单的方法可以做到这一点。创建对象的IL代码是newobj。为了实现这一点,您需要读取程序集中每个方法的 MethodBody,并识别包含 newobj 指令和您所追求的对象类型的类型操作数的方法。

DeclaringType will only tell you the enclosing type of the code model, but what you are after is identifying object creation point.

There is no easy way to do it other than reading the individual MethodBody IL. IL code for creating an object is newobj. To implement this, you will need to read the MethodBody for every method in your assembly and identify the method that contains newobj instruction with type operand of the object type you are after.

清风挽心 2024-09-22 04:01:05

通过使我的所有对象派生自具有父参数的自定义对象来解决。

Solved by making all my objects derive from a custom object with a parent parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文