我的对象知道它的所有者吗? [英] Does my Object know it's owner?
问题描述
我已经考虑了一段时间了.一个对象可以在没有直接引用(例如属性)的情况下知道它的所有者.示例:
I have been thinking about this for some time. Can an object know it''s owner without a direct reference like a property. Example:
Public Class foo 'owner
Private fBar As New foobar 'owned object - belongs to class foo
'fbar.Owner = foo 'direct property way
End Class
Public Class foobar
...
End Class
那么fBar是否有办法知道它的所有者/类别?反射可以做到吗?
So is there a way that fBar knows it''s owner/class? Can reflection do it?
推荐答案
否.如果引用了任何对象,则仅使用被引用对象是不够的,它可以获得此信息.这是编程的基本事实.反射在这里无济于事,因为这是数据,而不是元数据.
—SA
No. If any object is referenced, using referenced object along is not enough it get this information. This is a fundamental fact of programming. Reflection cannot help here, because this is data, not meta-data.
—SA
您可以尝试在同一Class内的以下代码获取声明了fBar
的Class的 Type .
You may try below code inside same Class to get Type of Class in whichfBar
is declared.
Public Class foo 'owner
Private fBar As New foobar 'owned object - belongs to class foo
'fbar.Owner = foo 'direct property way
Type t = GetType(Me)
End Class
Public Class foobar
...
End Class
请查看以下链接,以获取有关此内容的更多信息.
http://msdn.microsoft.com/en-us/library/system.object. gettype.aspx
http://msdn.microsoft.com/en-us/library/20fy88e0 (v = VS.100).aspx
当您动态参考Dll时,反射非常有用.我认为在这种情况下,您真的不需要反思.
Have a look at below links for more information on this.
http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx
http://msdn.microsoft.com/en-us/library/20fy88e0(v=VS.100).aspx
Reflection is useful when you are refferring to your Dll dynamically. I do not think in this case you really need reflection.
这篇关于我的对象知道它的所有者吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!