VB.Net2005
Simplified Code:
MustInherit Class InnerBase(Of Inheritor)
End Class
MustInherit Class OuterBase(Of Inheritor)
Class Inner
Inherits InnerBase(Of Inner)
End Class
End Class
Class ChildClass
Inherits OuterBase(Of ChildClass)
End Class
Class ChildClassTwo
Inherits OuterBase(Of ChildClassTwo)
End Class
MustInherit Class CollectionClass(Of _
Inheritor As CollectionClass(Of Inheritor, Member), _
Member As OuterBase(Of Member))
Dim fails As Member.Inner ' Type parameter cannot be used as qualifier
Dim works As New ChildClass.Inner
Dim failsAsExpected As ChildClassTwo.Inner = works ' type conversion failure
End Class
The error message on the "fails" line is in the subject, and "Member.Inner" is highlighted. Incidentally, the same error occurs with trying to call a shared method of OuterBase.
“失败”行上的错误消息在主题中,并且“Member.Inner”突出显示。顺便提一下,尝试调用OuterBase的共享方法时会出现同样的错误。
The "works" line works, but there are a dozen (and counting) ChildClass classes in real life.
“作品”系列有效,但现实生活中有十几个(计数)ChildClass课程。
The "failsAsExpected" line is there to show that, with generics, each ChildClass has its own distinct Inner class.
“failedAsExpected”行显示,使用泛型,每个ChildClass都有自己独特的Inner类。
My question: is there a way to get a variable, in class CollectionClass, defined as type Member.Inner? what's the critical difference that the compiler can't follow?
我的问题:有没有办法在类CollectionClass中获取变量,定义为类型Member.Inner?编译器无法遵循的关键区别是什么?
(I was eventually able to generate an object by creating a dummy object of type param and calling a method defined in OuterBase. Not the cleanest approach.)
(我最终能够通过创建param类型的虚拟对象并调用OuterBase中定义的方法来生成对象。不是最干净的方法。)
Edit 2008/12/2 altered code to make the two "base" classes generic.
编辑2008/12/2更改的代码,使两个“基础”类通用。
2 个解决方案
#1
1
Dim succeeds as OuterBase.Inner
.net does not have C++'s combination of template classes and typedefs, which means what you are trying to do is not possible, nor does it even make sense in .net.
.net没有C ++的模板类和typedef的组合,这意味着你想要做的事情是不可能的,甚至在.net中也没有意义。
#2
1
ChildClass.Inner
and SomeOtherChildClass.Inner
are the same type. Here's a short but complete program to demonstrate:
ChildClass.Inner和SomeOtherChildClass.Inner是相同的类型。这是一个简短但完整的程序来演示:
Imports System
MustInherit Class InnerBase
End Class
MustInherit Class OuterBase
Class Inner
Inherits InnerBase
End Class
End Class
Class ChildClass
Inherits OuterBase
End Class
Class OtherChildClass
Inherits OuterBase
End Class
Class Test
Shared Sub Main()
Dim x as new ChildClass.Inner
Dim y as new OtherChildClass.Inner
Console.WriteLine(x.GetType())
Console.WriteLine(y.GetType())
End Sub
End Class
The output is:
输出是:
OuterBase+Inner
OuterBase+Inner
What were you trying to achieve by using "parameterised" nested classes? I suspect that either it wouldn't work how you'd want it to, or you can achieve it just by using OuterBase.Inner
to start with.
你想通过使用“参数化”嵌套类来实现什么?我怀疑它不会像你想要的那样工作,或者你可以通过使用OuterBase.Inner来实现它。
Now if each of your child classes were declaring their own nested class, that would be a different situation - and one which generics wouldn't help you with.
现在,如果您的每个子类都声明了自己的嵌套类,那将是一种不同的情况 - 而且泛型不会帮助您。
#1
1
Dim succeeds as OuterBase.Inner
.net does not have C++'s combination of template classes and typedefs, which means what you are trying to do is not possible, nor does it even make sense in .net.
.net没有C ++的模板类和typedef的组合,这意味着你想要做的事情是不可能的,甚至在.net中也没有意义。
#2
1
ChildClass.Inner
and SomeOtherChildClass.Inner
are the same type. Here's a short but complete program to demonstrate:
ChildClass.Inner和SomeOtherChildClass.Inner是相同的类型。这是一个简短但完整的程序来演示:
Imports System
MustInherit Class InnerBase
End Class
MustInherit Class OuterBase
Class Inner
Inherits InnerBase
End Class
End Class
Class ChildClass
Inherits OuterBase
End Class
Class OtherChildClass
Inherits OuterBase
End Class
Class Test
Shared Sub Main()
Dim x as new ChildClass.Inner
Dim y as new OtherChildClass.Inner
Console.WriteLine(x.GetType())
Console.WriteLine(y.GetType())
End Sub
End Class
The output is:
输出是:
OuterBase+Inner
OuterBase+Inner
What were you trying to achieve by using "parameterised" nested classes? I suspect that either it wouldn't work how you'd want it to, or you can achieve it just by using OuterBase.Inner
to start with.
你想通过使用“参数化”嵌套类来实现什么?我怀疑它不会像你想要的那样工作,或者你可以通过使用OuterBase.Inner来实现它。
Now if each of your child classes were declaring their own nested class, that would be a different situation - and one which generics wouldn't help you with.
现在,如果您的每个子类都声明了自己的嵌套类,那将是一种不同的情况 - 而且泛型不会帮助您。