简体   繁体   中英

Get reference to main class from a property

I have a class Class1 with several properties (Property1, Property2, ...)

For some design reasons, I have access only to, let's say, Property1.

Is there a way to get a reference to Class1 ?

I tried Property1.Parent, Property1.Base but both failed.

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

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. And because of that, the same Property1Class instance can be referenced by more then one Class1 instances.

And your naming in misleading. Property1Class class does not derive from Class1 class. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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