" Option Strict On禁止从对象到T的隐式转换 (我在其他地方读到有关向上转换泛型的问题与 早期版本的c#。据推测,所有的IoC都可以绕过这个。Thx Tom"Option Strict On disallows implicit conversions from object to T"(I read elsewhere that there was a problem upcasting generics withearlier versions of c#. Presumably all the IoCs get round this though) 首先,让我道歉......我真的不明白你想要什么 完成 - 而且事实是,你不能从这里到达 - 至少不是你尝试这样做的方式。换句话说,你不能在运行时动态指定一个任何类型的casst - 并且Convert.ChangeType不会帮助你... 它基本上做的事情就像把字符串更改为数字等 - 基本上它是 处理实现IConvertable的类型。 也就是说,你可以肯定地走树。 ..但你需要做的事情 它类似于: Option Strict On Option Explicit On 选项推断关闭 进口系统 进口System.Reflection 模块模块1 Sub Main() Dim cc As New C() WalkInheritance(cc) End Sub $ /> Private Sub WalkInheritance(T作为BaseClass)(ByVal o As T) Console.WriteLine(o.GetType()。FullName) WalkInheritance(o,o.GetType()。BaseType) End Sub Private Sub WalkInheritance(Of T As BaseClass)( ByVal o As T, ByVal bType 作为类型) 如果不是bType什么都没有那么 Console.WriteLine(bType.FullName) bType = bType.BaseType WalkInheritance(o,bType) 结束如果 End Sub 私有MustInherit类BaseClass 结束类 私有类A 继承BaseClass b $ b结束班 私人班级B 继承A 结束班 私人C级 继承B 结束班级 结束模块 我希望这可以帮助你... - Tom SheltonFirst, let me apologize... I didn''t really understand what you were trying toaccomplish - and the fact is, you can''t get there from here - at least not theway your trying to do it. In other, words you can''t dynamically specify thetype of a casst at runtime - and Convert.ChangeType won''t help you there...It basically does stuff like changing strings to numbers, etc - basically itdeals with types that implement IConvertable.That said, you can definately walk the tree... But your going to have to doit something like:Option Strict OnOption Explicit OnOption Infer OffImports SystemImports System.ReflectionModule Module1Sub Main()Dim cc As New C()WalkInheritance(cc)End SubPrivate Sub WalkInheritance(Of T As BaseClass)(ByVal o As T)Console.WriteLine(o.GetType().FullName)WalkInheritance(o, o.GetType().BaseType)End SubPrivate Sub WalkInheritance(Of T As BaseClass)(ByVal o As T, ByVal bTypeAs Type)If Not bType Is Nothing ThenConsole.WriteLine(bType.FullName)bType = bType.BaseTypeWalkInheritance(o, bType)End IfEnd SubPrivate MustInherit Class BaseClassEnd ClassPrivate Class AInherits BaseClassEnd ClassPrivate Class BInherits AEnd ClassPrivate Class CInherits BEnd ClassEnd ModuleI hope this helps you some...--Tom Shelton 这篇关于行走的继承等级 - 为什么这不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!