Update1: it was like a duplicate before this update, but I think now it is quite specific, and the duplicate does not answer this edited question at all. Ok, so I found out that I should use a generic class with restrictions. The problem still remains about how I can restrict only classes derived from a class.
In practice, I want to be able to declare some abstract property with a type declared in a derived class, but limited only to derived types declared in the original class. Take this example:
Public Class ObjectBase End Class Public Class Derived1Class inherits ObjectClass Public Property1 as String End Class Public class Derived2Class inherits ObjectClass Public Property2 as string End Class Public Class SomeOtherClass(Of T as {OnlyClassesWhichInherits(ObjectBase)}) Public Property SomeProperty as T End Class Public Class SomeOtherDerivedClass inherits SomeOtherClass Sub New() SomeProperty = New Derived1Class SomeProperty.Property1:="this should be valid" End Sub End Class
The only way I know this can be achieved is to declare SomeProperty as Object (Update1: now I now call this function generic classes). If I declare SomeProperty as an ObjectBase, the compiler has no problem with SomeProperty = New DerivedClass, but it complains that Property1 is not a member of ObjectBase, it is true, but still it works with SomeProperty as an Object. This means that the behavior that interests me already exists in .NET and is implemented in the Object class (Update1: Generic Classes).
How to write a restriction on derived classes of ObjecBase?
source share