如何检测和识别返回对象的类型? [英] How to detect and identify the type of returned object?

查看:74
本文介绍了如何检测和识别返回对象的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下代码与基础Classe Toponyme和两个子类派生ToponymeFR和ToponymeBE

函数Detecter允许返回足够的派生类。



为您提供帮助。



Bernard





模块模块1



Sub Main()

Dim def作为新列表(Of字符串)

def.Add(法国)

def.Add(Belgique)

每个d字符串在def

Dim o As Object

o = Detecter(d)

Console.WriteLine(Objet =& o.ToString)

Console.WriteLine(Nom =& o.nom)

选择Case objectType'???????????

案例ToponymeFr

Console.WriteLine(CodeINSEE =& o.CodeINSEE)'??????????

Case ToponymeBe

Console.WriteLine(CodeINS =& o.CodeINS)'??????????

结束选择



下一页

Console.ReadLine()

End Sub

函数检测器(def as String)作为对象

选择案例def

CaseFrance

Dim topo As New ToponymeFr

返回topo

案例Belgique

Dim topo As New ToponymeBe

返回topo

Case Else

不返回

结束选择

结束功能



Public Class Toponyme

属性Def为字符串

属性Nom As String

物业代码为字符串

Public Sub New(d As String)

Def = d

End Sub

Public Sub New()



结束Sub $ / $
结束班级



公共类别ToponymeFr

继承Toponyme

物业CodeINSEE作为String

Public Sub New(d As String)

MyBase.New(d)

Nom =France

Code =Fr

CodeINSEE = d

End Sub

Public Sub New()

Nom =France

Code =Fr

End Sub



End Class



Public Class ToponymeBe

继承Toponyme

属性CodeINS为字符串

Public Sub New(d作为字符串)

MyBase.New(d)

Nom =Belgique

代码=是

CodeINS = d

End Sub

Public Sub New()

Nom =Belgique

Code =Be

End Sub

结束班级

结束模块



我尝试过的事情:



1)在侦探中我返回一个看起来不太好但我有的Objet选择?

2)ToponymeFr和ToponymeBe有一些不同的属性CodeINSEE和CodeINS,而无需在Main中访问它们。我试图找到一个方法ObjectType来识别返回的对象的类型,允许选择处理但没有成功。

解决方案

Object.GetType Method(System) [ ^ ]。

Hi,
I have the following code with a base Classe Toponyme and two sub-classes derived ToponymeFR et ToponymeBE
A function Detecter allows to return the adequate derived class.

Thnaks for your help.

Bernard


Module Module1

Sub Main()
Dim def As New List(Of String)
def.Add("France")
def.Add("Belgique")
For Each d As String In def
Dim o As Object
o = Detecter(d)
Console.WriteLine("Objet =" & o.ToString)
Console.WriteLine("Nom =" & o.nom)
Select Case objectType '???????????
Case ToponymeFr
Console.WriteLine("CodeINSEE=" & o.CodeINSEE) '???????????
Case ToponymeBe
Console.WriteLine("CodeINS=" & o.CodeINS) '???????????
End Select

Next
Console.ReadLine()
End Sub
Function Detecter(def As String) As Object
Select Case def
Case "France"
Dim topo As New ToponymeFr
Return topo
Case "Belgique"
Dim topo As New ToponymeBe
Return topo
Case Else
Return Nothing
End Select
End Function

Public Class Toponyme
Property Def As String
Property Nom As String
Property Code As String
Public Sub New(d As String)
Def = d
End Sub
Public Sub New()

End Sub
End Class

Public Class ToponymeFr
Inherits Toponyme
Property CodeINSEE As String
Public Sub New(d As String)
MyBase.New(d)
Nom = "France"
Code = "Fr"
CodeINSEE = d
End Sub
Public Sub New()
Nom = "France"
Code = "Fr"
End Sub

End Class

Public Class ToponymeBe
Inherits Toponyme
Property CodeINS As String
Public Sub New(d As String)
MyBase.New(d)
Nom = "Belgique"
Code = "Be"
CodeINS = d
End Sub
Public Sub New()
Nom = "Belgique"
Code = "Be"
End Sub
End Class
End Module

What I have tried:

1) In Detecter I Return an Objet which does not look very good but do I have the choice ?
2) ToponymeFr and ToponymeBe have some different properties CodeINSEE and CodeINS without having acces to them in Main. I have tried to find a method "ObjectType" to identify the type of the object returned allowing the choice of the treatment but without success.

解决方案

Object.GetType Method (System)[^].


这篇关于如何检测和识别返回对象的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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