简体   繁体   English

从属性获取对主类的引用

[英]Get reference to main class from a property

I have a class Class1 with several properties (Property1, Property2, ...) 我有一个带有多个属性的类Class1(Property1,Property2,...)

For some design reasons, I have access only to, let's say, Property1. 由于某些设计原因,我只能访问Property1。

Is there a way to get a reference to Class1 ? 有没有办法获得对Class1的引用?

I tried Property1.Parent, Property1.Base but both failed. 我尝试了Property1.Parent,Property1.Base,但是都失败了。

I'm using a heavy workaround which is creating a separate class for each property with a reference to the main class: 我正在使用繁重的解决方法,该方法是通过引用主类为每个属性创建一个单独的类:

Public Class DerivedProperty1
    Inherits Property1

    Public ParentClass as Class1

    Public Sub New(ParentClass as Class1)
        me.ParentClass = ParentClass
    End Sub
End Class

and then Class1 will become 然后Class1将成为

Public Class Class1
    Public DerivedProperty1 as DerivedProperty1
    ...
End Class

Is there another way different from this workaround ? 是否有另一种方法与此替代方法不同?

Thank you. 谢谢。

There is nothing build for that. 没有任何建树。

That's because the Class1.Property property really just hides a reference to a Property1Class class instance stored somewhere else. 这是因为Class1.Property属性实际上只是隐藏对存储在其他位置的Property1Class类实例的引用。 And because of that, the same Property1Class instance can be referenced by more then one Class1 instances. 因此,一个以上的Class1实例可以引用同一Property1Class实例。

And your naming in misleading. 和你的名字产生误导。 Property1Class class does not derive from Class1 class. Property1Class类不是从Class1类派生的。 It's just used by that class as a type of one of properties. 该类仅将其用作属性之一的类型。

So if you need that kind of functionalty, you have to code it by yourself. 因此,如果您需要这种功能,则必须自己编写代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM