行走的继承等级 - 为什么这不起作用? [英] Walking inheritance hierarchy - why doesn'y this work?

查看:56
本文介绍了行走的继承等级 - 为什么这不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有这个递归函数,我想继续使用

层次结构来设置字段值....


泛型T被约束为继承层次结构的基类


朋友共享函数InjectFieldValues(ByVal p_def为T,

ByVal p_properties As PropertyMaps)作为T


''....在这个继承层设置字段值的反射...


' 'walk inheritance hierachy在继承层上设置字段值

如果p_def.GetType.BaseType IsNot Nothing则

p_def = InjectFieldValues(CType(p_def,p_def.GetType.BaseType) ,_

p_properties)

结束如果


返回p_def


结束功能


CType抱怨p_def.GetType.BaseType没有定义 - 为什么?

当我不尝试演员而只是看看p_def .GetType.BaseType

在运行时,没关系。

还有另一种方法可以走层次结构吗?


Thx


S

Hi

I have this recursive function and I want to walk the inheritance
hierarchy to set field values ....

the generic T is constrainted as the base class of the inheritance hierarchy

Friend Shared Function InjectFieldValues(ByVal p_def As T,
ByVal p_properties As PropertyMaps) As T

'' .... reflection to set field values at this inheritance tier ...

''walk inheritance hierachy to set field values on inherited tiers
If p_def.GetType.BaseType IsNot Nothing Then
p_def = InjectFieldValues(CType(p_def, p_def.GetType.BaseType), _
p_properties)
End If

Return p_def

End Function

The CType complains that p_def.GetType.BaseType is not defined - why?
when I don''t try and cast but simply look at the p_def.GetType.BaseType
at runtime, it''s fine.

Is there another way to walk the hierarchy?

Thx

S

推荐答案

2008-06-20,Simon Woods< si ********* @ hotmail.comwrote:
On 2008-06-20, Simon Woods <si*********@hotmail.comwrote:




我有这个递归函数,我想继续使用

层次结构来设置字段值....


泛型T被约束为继承层次结构的基类


朋友共享函数InjectFieldValues(ByVal p_def为T,

ByVal p_properties As PropertyMaps)作为T


''....反映在此继承层设置字段值...


''walk inheritance hierachy在继承层上设置字段值

如果p_def.GetType.BaseType IsNot Nothing那么

p_def = InjectFieldValues(CType(p_def,p_def.GetType。 BaseType),_

p_properties)

结束如果


返回p_def


结束函数


CType抱怨p_def.GetType.BaseType没有定义 - 为什么?

当我不尝试演员而只是在运行时查看p_def.GetType.BaseType

时,没关系。


还有另一种走层次结构的方法吗?


Thx


S
Hi

I have this recursive function and I want to walk the inheritance
hierarchy to set field values ....

the generic T is constrainted as the base class of the inheritance hierarchy

Friend Shared Function InjectFieldValues(ByVal p_def As T,
ByVal p_properties As PropertyMaps) As T

'' .... reflection to set field values at this inheritance tier ...

''walk inheritance hierachy to set field values on inherited tiers
If p_def.GetType.BaseType IsNot Nothing Then
p_def = InjectFieldValues(CType(p_def, p_def.GetType.BaseType), _
p_properties)
End If

Return p_def

End Function

The CType complains that p_def.GetType.BaseType is not defined - why?
when I don''t try and cast but simply look at the p_def.GetType.BaseType
at runtime, it''s fine.

Is there another way to walk the hierarchy?

Thx

S



尝试Convert.ChangeType ...

Convert.ChangeType(p_def,p_def.GetType.BaseType)


-

Tom Shelton

Try Convert.ChangeType...

Convert.ChangeType (p_def, p_def.GetType.BaseType)

--
Tom Shelton


Tom Shelton写道:
Tom Shelton wrote:

2008-06-20,Simon Woods< si ********* @ hotmail.comwrote:
On 2008-06-20, Simon Woods <si*********@hotmail.comwrote:

> Hi

我有这个递归函数,我想走继承
层次结构来设置字段值....

通用T被约束为基类继承层次结构

朋友共享函数InjectFieldValues(ByVal p_def为T,
ByVal p_properties As PropertyMaps)作为T

''....在此继承层设置字段值的反射......

''walk inheritance hierachy在继承层上设置字段值
如果p_def.GetType.BaseType IsNot Nothing那么
p_def = InjectFieldValues(CType(p_def,p_def.GetType.BaseType),_
p_properties)
结束如果

返回p_def

结束函数

CType抱怨没有定义p_def.GetType.BaseType - 为什么?
当我不尝试演员但只是在运行时查看p_def.GetType.BaseType
时,没关系。

还有其他吗?走路的层次结构?

Thx

S
>Hi

I have this recursive function and I want to walk the inheritance
hierarchy to set field values ....

the generic T is constrainted as the base class of the inheritance hierarchy

Friend Shared Function InjectFieldValues(ByVal p_def As T,
ByVal p_properties As PropertyMaps) As T

'' .... reflection to set field values at this inheritance tier ...

''walk inheritance hierachy to set field values on inherited tiers
If p_def.GetType.BaseType IsNot Nothing Then
p_def = InjectFieldValues(CType(p_def, p_def.GetType.BaseType), _
p_properties)
End If

Return p_def

End Function

The CType complains that p_def.GetType.BaseType is not defined - why?
when I don''t try and cast but simply look at the p_def.GetType.BaseType
at runtime, it''s fine.

Is there another way to walk the hierarchy?

Thx

S



尝试Convert.ChangeType ...


Convert.ChangeType(p_def,p_def.GetType.BaseType)


Try Convert.ChangeType...

Convert.ChangeType (p_def, p_def.GetType.BaseType)



Thx Tom


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 with
earlier versions of c#. Presumably all the IoCs get round this though)



2008-06-20,Simon Woods< si ****** ***@hotmail.com写的:
On 2008-06-20, Simon Woods <si*********@hotmail.comwrote:

Tom Shelton写道:
Tom Shelton wrote:

> 2008-06 -20,Simon Woods< si ********* @ hotmail.comwrote:
>On 2008-06-20, Simon Woods <si*********@hotmail.comwrote:

>> Hi

我有这个递归函数,我想继续继承
层次结构来设置字段值....

通用T被约束为继承层次结构的基类

朋友共享函数InjectFieldValues(ByVal p_def为T,
ByVal p_properties As PropertyMaps)作为T

''....在此继承层设置字段值的反射。 ..

''继承层次结构在继承层上设置字段值
如果p_def.GetType.BaseType IsNot Nothing那么
p_def = InjectFieldValues(CType(p_de) f,p_def.GetType.BaseType),_
p_properties)
结束如果

返回p_def

结束功能

CType抱怨p_def.GetType.BaseType没有定义 - 为什么?
当我不尝试演员但只是在运行时查看p_def.GetType.BaseType
时,它很好。

还有其他方法可以走层次吗?

Thx

S
>>Hi

I have this recursive function and I want to walk the inheritance
hierarchy to set field values ....

the generic T is constrainted as the base class of the inheritance hierarchy

Friend Shared Function InjectFieldValues(ByVal p_def As T,
ByVal p_properties As PropertyMaps) As T

'' .... reflection to set field values at this inheritance tier ...

''walk inheritance hierachy to set field values on inherited tiers
If p_def.GetType.BaseType IsNot Nothing Then
p_def = InjectFieldValues(CType(p_def, p_def.GetType.BaseType), _
p_properties)
End If

Return p_def

End Function

The CType complains that p_def.GetType.BaseType is not defined - why?
when I don''t try and cast but simply look at the p_def.GetType.BaseType
at runtime, it''s fine.

Is there another way to walk the hierarchy?

Thx

S


试试转换.ChangeType ......

Convert.ChangeType(p_def,p_def.GetType.BaseType)


Try Convert.ChangeType...

Convert.ChangeType (p_def, p_def.GetType.BaseType)



Thx Tom

" 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 with
earlier 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 < br $> b $ b结束班


私人班级B

继承A

结束班


私人C级

继承B

结束班级

结束模块


我希望这可以帮助你...


-

Tom Shelton

First, let me apologize... I didn''t really understand what you were trying to
accomplish - and the fact is, you can''t get there from here - at least not the
way your trying to do it. In other, words you can''t dynamically specify the
type of a casst at runtime - and Convert.ChangeType won''t help you there...
It basically does stuff like changing strings to numbers, etc - basically it
deals with types that implement IConvertable.

That said, you can definately walk the tree... But your going to have to do
it something like:

Option Strict On
Option Explicit On
Option Infer Off

Imports System
Imports System.Reflection

Module Module1

Sub Main()
Dim cc As New C()
WalkInheritance(cc)
End Sub

Private Sub WalkInheritance(Of T As 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
As Type)
If Not bType Is Nothing Then
Console.WriteLine(bType.FullName)
bType = bType.BaseType
WalkInheritance(o, bType)

End If
End Sub

Private MustInherit Class BaseClass

End Class

Private Class A
Inherits BaseClass
End Class

Private Class B
Inherits A
End Class

Private Class C
Inherits B
End Class
End Module

I hope this helps you some...

--
Tom Shelton


这篇关于行走的继承等级 - 为什么这不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆