C#:反射:获取创建的类对象
我正在研究如何获取创建另一个对象的对象(或对象类型)。例如:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不 - 此信息不会存储在任何地方。请注意,即使是这样,它也很容易过时。例如:
现在应该显示什么?
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:
What should that now show? The current value of
BObject.A_object
was created asrootAObject
, but the current value ofrootAObject
is null. If you'd want that to showBObject
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?
我相信您正在寻找的是在您感兴趣的
System.Type
实例上定义的属性DeclaringType
。请参阅 DeclaringType 文档。I believe what you're looking for is the property
DeclaringType
, defined on theSystem.Type
instance that you're interested in. See DeclaringType documentation.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.
通过使我的所有对象派生自具有父参数的自定义对象来解决。
Solved by making all my objects derive from a custom object with a parent parameter.