By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,799 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,799 IT Pros & Developers. It's quick & easy.

Using CType to Cast to Variable Type

P: n/a
I have the following lines

Dim t As Type = GetType(MyType)
Dim serialiser As New XmlSerializer(t)

I want to be able to do the following with a FileStream fs

Dim instance As MyType

instance = CType(serialiser.Deserialize(fs), MyType)

but replacing MyType with something more generic, for example

instance = CType(serialiser.Deserialize(fs), t)

Obviously, this does not compile, but could someone tell me what I should
put as the second parameter to CType to make it work? I have tried all
sorts, but just get a selection of compile time errors.

The object of the excercise is to have a shared method of a base class that
deserialises (and serialises) the class.

TIA

Charles
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
If instance is of type MyType the it only makes sense to use CType to cast
the desterilized stream into MyType as below:

instance = CType(serialiser.Deserialize(fs), MyType)

If you are trying to create a common function to desterilize different types
of objects then you might need to look into the object type.

"Charles Law" <bl***@nowhere.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
I have the following lines

Dim t As Type = GetType(MyType)
Dim serialiser As New XmlSerializer(t)

I want to be able to do the following with a FileStream fs

Dim instance As MyType

instance = CType(serialiser.Deserialize(fs), MyType)

but replacing MyType with something more generic, for example

instance = CType(serialiser.Deserialize(fs), t)

Obviously, this does not compile, but could someone tell me what I should
put as the second parameter to CType to make it work? I have tried all
sorts, but just get a selection of compile time errors.

The object of the excercise is to have a shared method of a base class that deserialises (and serialises) the class.

TIA

Charles

Nov 21 '05 #2

P: n/a
Sorry.. hit send too soon there.. as I was saying...

Deserialize to object in the shared function thern cast afterwards once you
know the type.

Dim instance As Object

instance = CType(serialiser.Deserialize(fs), Object)

.. . .

Dim var As MyType

var = DirectCast(instance, MyType)

Thsi moves the actual casting code into the code that really knows what the
MyType IS. You can catch an InvaklidCast Exception if soemeone tries to cast
to a type that is different...

Or, you can maybe try setitng up your shared function to take a string name
of the type you want to deserialze and use that..

"Ray Cassick" <rc******@nospam.enterprocity.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If instance is of type MyType the it only makes sense to use CType to cast
the desterilized stream into MyType as below:

instance = CType(serialiser.Deserialize(fs), MyType)

If you are trying to create a common function to desterilize different types of objects then you might need to look into the object type.

"Charles Law" <bl***@nowhere.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
I have the following lines

Dim t As Type = GetType(MyType)
Dim serialiser As New XmlSerializer(t)

I want to be able to do the following with a FileStream fs

Dim instance As MyType

instance = CType(serialiser.Deserialize(fs), MyType)

but replacing MyType with something more generic, for example

instance = CType(serialiser.Deserialize(fs), t)

Obviously, this does not compile, but could someone tell me what I should put as the second parameter to CType to make it work? I have tried all
sorts, but just get a selection of compile time errors.

The object of the excercise is to have a shared method of a base class

that
deserialises (and serialises) the class.

TIA

Charles


Nov 21 '05 #3

P: n/a
Hi Ray

Thanks for the suggestions. I think I may have come up with a satisfactory
way to do it. It requires a bit of code in each inheriting class, but also
works if the function is not shadowed.

<code>
Public MustInherit Class MyTypeBase

Public Function Deserialise() As Object

Return New Object

End Function

End Class

Public Class MyType

Inherits MyTypeBase

Public Shadows Function Deserialise() As MyType

Return DirectCast(MyBase.Deserialise, MyType)

End Function

End Class
</code>

Charles
"Ray Cassick" <rc******@nospam.enterprocity.com> wrote in message
news:OH**************@TK2MSFTNGP09.phx.gbl...
Sorry.. hit send too soon there.. as I was saying...

Deserialize to object in the shared function thern cast afterwards once
you
know the type.

Dim instance As Object

instance = CType(serialiser.Deserialize(fs), Object)

. . .

Dim var As MyType

var = DirectCast(instance, MyType)

Thsi moves the actual casting code into the code that really knows what
the
MyType IS. You can catch an InvaklidCast Exception if soemeone tries to
cast
to a type that is different...

Or, you can maybe try setitng up your shared function to take a string
name
of the type you want to deserialze and use that..

"Ray Cassick" <rc******@nospam.enterprocity.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If instance is of type MyType the it only makes sense to use CType to
cast
the desterilized stream into MyType as below:

instance = CType(serialiser.Deserialize(fs), MyType)

If you are trying to create a common function to desterilize different

types
of objects then you might need to look into the object type.

"Charles Law" <bl***@nowhere.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
> I have the following lines
>
> Dim t As Type = GetType(MyType)
> Dim serialiser As New XmlSerializer(t)
>
> I want to be able to do the following with a FileStream fs
>
> Dim instance As MyType
>
> instance = CType(serialiser.Deserialize(fs), MyType)
>
> but replacing MyType with something more generic, for example
>
> instance = CType(serialiser.Deserialize(fs), t)
>
> Obviously, this does not compile, but could someone tell me what I should > put as the second parameter to CType to make it work? I have tried all
> sorts, but just get a selection of compile time errors.
>
> The object of the excercise is to have a shared method of a base class

that
> deserialises (and serialises) the class.
>
> TIA
>
> Charles
>
>



Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.