dynamic casting

Z

zino

how can I get the class type from an interface supposing that the class
implement this interface as below:

Public Interface myInterface
'... ... some methods
End Interface

public class myclass
Implements myInterface

'... ... some methods
end class


function myFunction
dim myInt As myInterface = New myclass
dim t As Type = myInt.GetType



' I need something that is equivalent to this statement (trying to get
"myclass"
' type) from the interface. Is there a way to get "myClass" type, from
t ?
'dim myList As New List(Of myclass)

' but this does not compile
dim myList as new List(of t)
end function
 
A

Anthony Jones

zino said:
how can I get the class type from an interface supposing that the class
implement this interface as below:

Public Interface myInterface
'... ... some methods
End Interface

public class myclass
Implements myInterface

'... ... some methods
end class


function myFunction
dim myInt As myInterface = New myclass
dim t As Type = myInt.GetType



' I need something that is equivalent to this statement (trying to get
"myclass"
' type) from the interface. Is there a way to get "myClass" type, from
t ?
'dim myList As New List(Of myclass)

' but this does not compile
dim myList as new List(of t)
end function

What you are trying to do is determine the concrete type of a generic at
runtime. This not possible, that would make VB.NET a dynamic language which
it isn't. Generic types are resolved at compilie time. Hence what you are
trying to do is not possible.

Can you not make myFunction a generic method?

Alternatively why not List(Of myInterface) ?
 
Z

zino66

I couldn't do "List(Of myInterface) " because of the following:


Interface myInterface
function GetDataSource(ByVal xmlString As String) As IEnumerable(Of
myInterface)
end interface

myClass1
implements myInterface

function GetDataSource implements myInterface.DataSource
'... .. . internalContact is a class
Dim xDoc As XDocument = XDocument.Parse(xmlString)
Dim docs = From d In xDoc.<root>.Elements _
Select New internalContact(d.<int>.@account) _
With {.type= d.@<business>}

Return docs
end class

myClass2
implements myInterface

function GetDataSource implements myInterface.DataSource
'... .. . ExternalContact is a class
Dim xDoc As XDocument = XDocument.Parse(xmlString)
Dim docs = From d In xDoc.<root>.Elements _
Select New ExternalContact(d.<ext>.@name _
With {.City = d.@<city>}

Return docs
end class

in Silverlight
Page.XAML:

function populateGrid(xmlString as string, t as type)
dim myInt as myInterface
if typeof(t) is myClass1 then
myInt = new myClass1
else
myInt= new myClass2
end

' this does not work
dim myList As new List(Of myInterface)
myList = myInt.GetDataSource(xmlString).toList

myGrid.ItemsSource = myList
end function

any idea how the interface should be implemented in order to achieve that ?
thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top