当前强制转换与对象类型 [英] Current Cast vs. Object Type

查看:74
本文介绍了当前强制转换与对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定对象的当前强制转换是什么。

基本上,如果我使用GetType,它总是返回原始类型

对象创建的对象但我想知道

对象的当前演员是什么(即:它仍然被演员为派生类,还是演员为

a基类?)这里有一些伪代码来展示这个例子。


Class Base


....

End Class


类派生:继承基础


....

结束班

Public Sub Main()

Dim derived1 as New Derived()

Dim base1 as Base


base1 = Ctype(derived1,Base)


Console.Writeline(base1.GetType()。ToString())

End Sub


''输出将是Derived。如何确定CAST是什么?即

基础?


提前致谢。


Dinsdale

解决方案

" Dinsdale" < ru ******** @ gmail.comschrieb


我正在尝试确定对象的当前演员是什么。

基本上,如果我使用GetType,它总是返回创建的对象的原始类型

,但我想知道对象的当前强制转换是什么?即:它仍然作为派生类进行演员,还是作为基类投射?
?这里有一些伪代码来展示这个例子。


班级基础


...

结束班级


类派生:继承基础
< br $> b $ b ...

结束班


Public Sub Main()

Dim derived1 as New Derived ()

Dim base1 as Base


base1 = Ctype(derived1,Base)


Console.Writeline( base1.GetType()。ToString())

End Sub


''输出将是Derived。如何确定CAST是什么?

即基地?


提前致谢。


Dinsdale



为什么你想知道这个?无论如何,编译器允许使用

的参考。我不敢说看看昏暗的声明。 ;-)


使用局部变量这是不可能的。对于字段,您可以使用

反射(为什么)并查看System.Type.DeclaringType。

Armin


你想做什么?您可以使用Reflection获得基本类型或祖先类型




严格来说,一旦将变量转换为<,就不能这样做br />
另一个变量,你没有任何关于它来自何处的信息。

基本上这有点像你想知道整数变量是否是

从代码中的常量创建或从字符串解析或取自

另一个变量等....


IMO的第一步将解释你想要做什么,以便我们

可以理解反射是否足够或者是否有人可以建议

另一种方式做任何你想做的事情做。


---

Patrice


" Dinsdale" < ru ******** @gmail.comaécritdansle message de news:
11 ******** @ n60g2000hse.googlegroups。 com ...


>我正在尝试确定当前对象的演员是什么。

基本上,如果我使用GetType,它总是返回对象创建的原始类型

但是我想知道

对象的当前强制转换是什么(即:它是否仍然是作为派生类演员,还是演员为

a基类?)这里有一些伪代码来展示这个例子。


Class Base


...

结束班级


类派生:继承基础


...

结束班


Public Sub Main()

Dim derived1 as New Derived()

Dim base1 as Base


base1 = Ctype(derived1,Base)


Console.Writeline(base1.GetType()。 ToString())


End Sub


''输出将是Derived。如何确定CAST是什么?即

基础?


提前致谢。


Dinsdale



6月28日上午10点35分,Armin Zingler < az.nos ... @ freenet.dewrote:


" Dinsdale" < russ.ha ... @ gmail.comschrieb


我正在尝试确定对象的当前演员是什么。

基本上,如果我使用GetType,它总是返回创建的对象的原始类型

,但我想知道对象的当前转换为
(即:是它仍然作为Derived类强制转换,还是作为基类转换为
?)这里有一些伪代码来展示这个例子。


Class Base


...

结束类


类派生:继承基数


...

结束等级


Public Sub Main()

Dim derived1 as New派生()

Dim base1 as Base


base1 = Ctype(derived1,Base)


Console.Writeline(base1.GetType()。ToString())


End Sub


''输出将是Derived。如何确定CAST是什么?

即Base?


先谢谢。


Dinsdale



为什么你想知道这个?无论如何,编译器允许使用

的参考。我不敢说看看昏暗的声明。 ;-)


使用局部变量这是不可能的。对于字段,您可以使用

反射(为什么)并查看System.Type.DeclaringType。


Armin



雅,我等着有人这么说。 lol。


数据库使用表格中所有主键的序列号

,我们正在尝试围绕它创建一个类库。我不希望

必须将派生类编码为base_id,derived_id,

derived_2_id所以我创建了一组标识符(包括

并发标识符,例如修改日期)。然后最低的基础

类有一个ID属性,它查看类型并从该集合中返回

正确的标识符值。如上所述,

问题是GetType()没有这样做。以下是

代码不起作用。 :(


公共财产ID()为整数

获取

CheckIdentifiers()''确保实际上有标识符

Dim typeName As String

typeName = Me.GetType.Name

返回标识符(typeName).ID

结束获取

设置(ByVal值为整数)

CheckIdentifiers()

Dim typeName As String

typeName = Me.GetType.Name

标识符(typeName).ID = value

结束集

结束财产

所以,如果我创建DerivedType2的对象并想将其转换为

BaseType然后获取BaseType.ID属性,则上面的代码

将作为DerivedType2执行,所以我得到错误的ID。


不幸的是,在我的测试应用程序中,DeclaringType即将出现

没什么,但感谢您的帮助。它让我有所作为在...


干杯

Dinsdale


I am trying to determine what the current cast of an object is.
Essentially, if I use GetType, it always returns the original type of
the object created but I want to know what the current cast of the
object is (i.e.: is it still cast as a Derived class, or is it cast as
a Base class?) Here is some pseudo code to show the example.

Class Base

....
End Class

Class Derived : Inherits Base

....
End Class

Public Sub Main()
Dim derived1 as New Derived()
Dim base1 as Base

base1 = Ctype(derived1, Base)

Console.Writeline (base1.GetType().ToString())

End Sub

''Output would be "Derived". How to I determine what the CAST is? i.e.
Base?

Thanks in advance.

Dinsdale

解决方案

"Dinsdale" <ru********@gmail.comschrieb

I am trying to determine what the current cast of an object is.
Essentially, if I use GetType, it always returns the original type
of the object created but I want to know what the current cast of
the object is (i.e.: is it still cast as a Derived class, or is it
cast as a Base class?) Here is some pseudo code to show the example.

Class Base

...
End Class

Class Derived : Inherits Base

...
End Class

Public Sub Main()
Dim derived1 as New Derived()
Dim base1 as Base

base1 = Ctype(derived1, Base)

Console.Writeline (base1.GetType().ToString())

End Sub

''Output would be "Derived". How to I determine what the CAST is?
i.e. Base?

Thanks in advance.

Dinsdale


Why would you want to know this? The compiler permits what''s possible with
the reference anyway. I don''t dare to say "look at the dim statement" ;-)

With local variables this is not possible. With fields, you could use
reflection (whyever) and have a look at System.Type.DeclaringType.
Armin


What are you trying to do ? You could get the base type or an ancestor type
using Reflection.

Strictly speaking you can''t do that as once you casted the variable to
another variable you don''t have any information about where it came from.
Basically this is bit as if you wanted to know if an integer variable was
created from a constant in the code or parsed from a string or taken from
another variable etc....

IMO the first step would be to explain what you are trying to do so that we
can understand if reflection would be enough or if someone can suggest
another way of doing whatever you are trying to do.

---
Patrice

"Dinsdale" <ru********@gmail.coma écrit dans le message de news:
11**********************@n60g2000hse.googlegroups. com...

>I am trying to determine what the current cast of an object is.
Essentially, if I use GetType, it always returns the original type of
the object created but I want to know what the current cast of the
object is (i.e.: is it still cast as a Derived class, or is it cast as
a Base class?) Here is some pseudo code to show the example.

Class Base

...
End Class

Class Derived : Inherits Base

...
End Class

Public Sub Main()
Dim derived1 as New Derived()
Dim base1 as Base

base1 = Ctype(derived1, Base)

Console.Writeline (base1.GetType().ToString())

End Sub

''Output would be "Derived". How to I determine what the CAST is? i.e.
Base?

Thanks in advance.

Dinsdale



On Jun 28, 10:35 am, "Armin Zingler" <az.nos...@freenet.dewrote:

"Dinsdale" <russ.ha...@gmail.comschrieb

I am trying to determine what the current cast of an object is.
Essentially, if I use GetType, it always returns the original type
of the object created but I want to know what the current cast of
the object is (i.e.: is it still cast as a Derived class, or is it
cast as a Base class?) Here is some pseudo code to show the example.

Class Base

...
End Class

Class Derived : Inherits Base

...
End Class

Public Sub Main()
Dim derived1 as New Derived()
Dim base1 as Base

base1 = Ctype(derived1, Base)

Console.Writeline (base1.GetType().ToString())

End Sub

''Output would be "Derived". How to I determine what the CAST is?
i.e. Base?

Thanks in advance.

Dinsdale


Why would you want to know this? The compiler permits what''s possible with
the reference anyway. I don''t dare to say "look at the dim statement" ;-)

With local variables this is not possible. With fields, you could use
reflection (whyever) and have a look at System.Type.DeclaringType.

Armin

Ya, I was waiting for someone to say that. lol.

The database uses a sequence number for all primary keys in the table
and we are trying to create a class library around it. I didn''t want
to have to code derived classes as having base_id, derived_id,
derived_2_id so I''ve created a collection of identifiers (including
concurrency identifiers such as modified date). Then the lowest base
class has an ID property that looks at the type and returns the
correct Identifier value from the collection for that cast. The
problem is GetType() doesn''t do this, as noted above. The following is
the code that doesn''t work. :(

Public Property ID() As Integer
Get
CheckIdentifiers() ''Ensures that there are in fact identifiers
Dim typeName As String
typeName = Me.GetType.Name
Return Identifiers(typeName).ID
End Get
Set(ByVal value As Integer)
CheckIdentifiers()
Dim typeName As String
typeName = Me.GetType.Name
Identifiers(typeName).ID = value
End Set
End Property
So, If I create an object of DerivedType2 and want to cast it as a
BaseType and then get the BaseType.ID property, the above code
executes as DerivedType2 so I get the wrong ID.

Unfortunately, in my test app the DeclaringType is coming up as
Nothing, but thanks for the help. It gives me something to work on...

Cheers
Dinsdale


这篇关于当前强制转换与对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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