Inherits Object not able to be cast

  • Thread starter Thread starter Random
  • Start date Start date
R

Random

I know the subject line is a little confusing, but I always try to use words
that are searchable for anyone else having a similar problem.

Here's what I've got...

I'm retreiving an XML string from a Web Service that represents a serialized
custom class [MyCustomClass]. On my client, I have a class that inherits
from that custom class [MyInheritsCustomClass], and contains just one new
property. Once I get the XML string from the WebMethod, I want to
Deserialize it into the client's class that inherits from the original
MyCustomClass.

Problems I'm running into...

1) The statement...

Dim ser As New XmlSerializer(GetType(MyInheritsCustomClass))

....gives me an "There was an error reflecting type 'MyInheritsCustomClass'"
exception

If I change this to...

Dim ser As New XmlSerializer(GetType(MyCustomClass))

.... I can create the XmlSerializer fine, but then I can't cast the
deserialized MyCustomClass object I get into a MyInheritsCustomClass object.

Am I missing something? Or is this just not possible for some reason?
 
It is possible if you mark your base class and the inherited class with the
XmlRoot attribute. I.e. (I hope it is ok that I am giving the example in C#):

[XmlRoot(Namespace = "www.domain.com", ElementName = "MyClassName")]
public class MyInheritsCustomClass
{
// ...
}

[XmlRoot(Namespace = "www.domain.com", ElementName = "MyClassName")]
public class MyCustomClass
{
// ...
}

You can now construct XmlSerializers to serialize and deserialize with the
following code:

// Serialize
XmlSerializer serial = new XmlSerializer(typeof(MyCustomClass));

// Deserialize
XmlSerializer serial = new XmlSerializer(typeof(MyInheritsCustomClass));

Hope, this helps.

Regards, Jakob.
 
I translated your example into VB, but unfortunately, it did not work. I
still had the same "error reflecting type" message. Interestingly, if I
left in the XmlRoot declaration on the MyCustomClass, and tried to
desreialize it instead of the MyInheritsCustomClass, I get an "error in XML
document (1, 2)" error. I take it out, I can deserialize it.



Jakob Christensen said:
It is possible if you mark your base class and the inherited class with the
XmlRoot attribute. I.e. (I hope it is ok that I am giving the example in C#):

[XmlRoot(Namespace = "www.domain.com", ElementName = "MyClassName")]
public class MyInheritsCustomClass
{
// ...
}

[XmlRoot(Namespace = "www.domain.com", ElementName = "MyClassName")]
public class MyCustomClass
{
// ...
}

You can now construct XmlSerializers to serialize and deserialize with the
following code:

// Serialize
XmlSerializer serial = new XmlSerializer(typeof(MyCustomClass));

// Deserialize
XmlSerializer serial = new XmlSerializer(typeof(MyInheritsCustomClass));

Hope, this helps.

Regards, Jakob.


Random said:
I know the subject line is a little confusing, but I always try to use words
that are searchable for anyone else having a similar problem.

Here's what I've got...

I'm retreiving an XML string from a Web Service that represents a serialized
custom class [MyCustomClass]. On my client, I have a class that inherits
from that custom class [MyInheritsCustomClass], and contains just one new
property. Once I get the XML string from the WebMethod, I want to
Deserialize it into the client's class that inherits from the original
MyCustomClass.

Problems I'm running into...

1) The statement...

Dim ser As New XmlSerializer(GetType(MyInheritsCustomClass))

....gives me an "There was an error reflecting type 'MyInheritsCustomClass'"
exception

If I change this to...

Dim ser As New XmlSerializer(GetType(MyCustomClass))

.... I can create the XmlSerializer fine, but then I can't cast the
deserialized MyCustomClass object I get into a MyInheritsCustomClass object.

Am I missing something? Or is this just not possible for some reason?
 
The "error in XML document (1,2)" message is caused because the XML
does not match the schema needed to deserialize into
MyInheritsCustomClass. In other words, the root element found at
(1,2) does not match the expected root element. The XML returned from
your web service follows the schema for the class structure
MyCustomClass. To deserialize into an instance of
MyInheritsCustomClass, you must make sure the XML schema for the
MyInheritsCustomClass matches the schema for MyCustomClass. Jakob's
reply has example of this but there is one bug in the example. He
forgot to inherit from MyCustomClass in MyInheritsCustomClass.

public class MyCustomClass
{
// ...
}

[XmlRoot( "MyCustomClass" )]
public class MyInheritsCustomClass : MyCustomClass
{
// ...
}

Please post your class structures so we can see what the problem is if
this still does not work for you.

-KIRBY


I translated your example into VB, but unfortunately, it did not work. I
still had the same "error reflecting type" message. Interestingly, if I
left in the XmlRoot declaration on the MyCustomClass, and tried to
desreialize it instead of the MyInheritsCustomClass, I get an "error in XML
document (1, 2)" error. I take it out, I can deserialize it.



Jakob Christensen said:
It is possible if you mark your base class and the inherited class with the
XmlRoot attribute. I.e. (I hope it is ok that I am giving the example in C#):

[XmlRoot(Namespace = "www.domain.com", ElementName = "MyClassName")]
public class MyInheritsCustomClass
{
// ...
}

[XmlRoot(Namespace = "www.domain.com", ElementName = "MyClassName")]
public class MyCustomClass
{
// ...
}

You can now construct XmlSerializers to serialize and deserialize with the
following code:

// Serialize
XmlSerializer serial = new XmlSerializer(typeof(MyCustomClass));

// Deserialize
XmlSerializer serial = new XmlSerializer(typeof(MyInheritsCustomClass));

Hope, this helps.

Regards, Jakob.


Random said:
I know the subject line is a little confusing, but I always try to use words
that are searchable for anyone else having a similar problem.

Here's what I've got...

I'm retreiving an XML string from a Web Service that represents a serialized
custom class [MyCustomClass]. On my client, I have a class that inherits
from that custom class [MyInheritsCustomClass], and contains just one new
property. Once I get the XML string from the WebMethod, I want to
Deserialize it into the client's class that inherits from the original
MyCustomClass.

Problems I'm running into...

1) The statement...

Dim ser As New XmlSerializer(GetType(MyInheritsCustomClass))

....gives me an "There was an error reflecting type 'MyInheritsCustomClass'"
exception

If I change this to...

Dim ser As New XmlSerializer(GetType(MyCustomClass))

.... I can create the XmlSerializer fine, but then I can't cast the
deserialized MyCustomClass object I get into a MyInheritsCustomClass object.

Am I missing something? Or is this just not possible for some reason?
 
Alrighty, here's more complete code...
----------------------------------------
'MyCustomClass
Namespace MyOrg.SEED
<Serializable()> Public Class User
<XmlElementAttribute(ElementName:="Agency", Type:=GetType(_AgencyFull))>
Public Agency() As _AgencyFull
<XmlAttributeAttribute()> Public ID As String
<XmlAttributeAttribute()> Public nID As String
<XmlAttributeAttribute()> Public Name As String
<XmlAttributeAttribute()> Public StartDate As String
<XmlAttributeAttribute()> Public Status As String
<Serializable()> Public Class _AgencyFull
Inherits _Agency
<XmlElementAttribute(ElementName:="Address",
Type:=GetType(_Address))> Public Address As _Address
End Class
End Class


<Serializable()> Public MustInherit Class _Agency
<XmlAttributeAttribute()> Public ID As String
<XmlAttributeAttribute()> Public Name As String
<XmlAttributeAttribute()> Public Primary As Boolean
End Class

<Serializable()> Public Class _Address
<XmlAttributeAttribute(AttributeName:="line1")> Public Line1 As String
<XmlAttributeAttribute(AttributeName:="line2")> Public Line2 As String
<XmlAttributeAttribute(AttributeName:="city")> Public City As String
<XmlAttributeAttribute(AttributeName:="state")> Public State As String
<XmlAttributeAttribute(AttributeName:="postalcode")> Public ZipCode As
String
<XmlAttributeAttribute(AttributeName:="country")> Public Country As
String
End Class

End Namespace


----------------------------------------
'MyInheritsCustomClass
NameSpace TheirOrg.SEED

<Serializable()> Public Class User
Inherits MyOrg.SEED.User
<NonSerialized()> Public XML As String
End Class

End Namespace

'In the application defining the MyInheritsCustomClass
(TheirOrg.SEED.User), I would have a function that retrieves the
deserializable XML from a Web Service, and deserializes it into the derived
class for return...

Public Function LoadUser(ByVal username As String) As TheirOrg.SEED.User
Dim oUser as New TheirOrg.SEED.User
Dim sXML As String = MyWebServiceProxy.GetUser(username)
Dim mynull As XmlParserContext
Dim rXML As XmlReader = New XmlTextReader(sXML, XmlNodeType.Document,
mynull)
Dim xSer As New XmlSerializer(GetType(TheirOrg.SEED.User))
oUser = xSer.Deserialize(rXML)
oUser.XML = sXML
Return oUser
End Function

The XML document looks like this...

<User ID="23" nID="1 " Name="User X"
StartDate="2004-07-19T10:01:13.300" Status="XA">
<Agency ID="56EXD" Name="Organization X" Primary="1"/>
</User>

Forget that the Agency address is not represented in the XML, it's omission
does not affect the deserialization. So, the error I'm getting is in the
line...

Dim xSer As New XmlSerializer(GetType(TheirOrg.SEED.User))

If I change the 'TheirOrg' to 'MyOrg' in the function, and comment out the
"oUser.XML = sXML", the Myorg.SEED.User class is returned just fine. Adding
in the <XmlRoot> clause in both namespaces does not change the error when I
try to initialize the XmlSerializer.

Hope this sheds some more light on the problem.

Kirby Turner said:
The "error in XML document (1,2)" message is caused because the XML
does not match the schema needed to deserialize into
MyInheritsCustomClass. In other words, the root element found at
(1,2) does not match the expected root element. The XML returned from
your web service follows the schema for the class structure
MyCustomClass. To deserialize into an instance of
MyInheritsCustomClass, you must make sure the XML schema for the
MyInheritsCustomClass matches the schema for MyCustomClass. Jakob's
reply has example of this but there is one bug in the example. He
forgot to inherit from MyCustomClass in MyInheritsCustomClass.

public class MyCustomClass
{
// ...
}

[XmlRoot( "MyCustomClass" )]
public class MyInheritsCustomClass : MyCustomClass
{
// ...
}

Please post your class structures so we can see what the problem is if
this still does not work for you.

-KIRBY


I translated your example into VB, but unfortunately, it did not work. I
still had the same "error reflecting type" message. Interestingly, if I
left in the XmlRoot declaration on the MyCustomClass, and tried to
desreialize it instead of the MyInheritsCustomClass, I get an "error in XML
document (1, 2)" error. I take it out, I can deserialize it.



Jakob Christensen said:
It is possible if you mark your base class and the inherited class with the
XmlRoot attribute. I.e. (I hope it is ok that I am giving the example
in
C#):
[XmlRoot(Namespace = "www.domain.com", ElementName = "MyClassName")]
public class MyInheritsCustomClass
{
// ...
}

[XmlRoot(Namespace = "www.domain.com", ElementName = "MyClassName")]
public class MyCustomClass
{
// ...
}

You can now construct XmlSerializers to serialize and deserialize with the
following code:

// Serialize
XmlSerializer serial = new XmlSerializer(typeof(MyCustomClass));

// Deserialize
XmlSerializer serial = new XmlSerializer(typeof(MyInheritsCustomClass));

Hope, this helps.

Regards, Jakob.


:

I know the subject line is a little confusing, but I always try to
use
words
that are searchable for anyone else having a similar problem.

Here's what I've got...

I'm retreiving an XML string from a Web Service that represents a serialized
custom class [MyCustomClass]. On my client, I have a class that inherits
from that custom class [MyInheritsCustomClass], and contains just one new
property. Once I get the XML string from the WebMethod, I want to
Deserialize it into the client's class that inherits from the original
MyCustomClass.

Problems I'm running into...

1) The statement...

Dim ser As New XmlSerializer(GetType(MyInheritsCustomClass))

....gives me an "There was an error reflecting type 'MyInheritsCustomClass'"
exception

If I change this to...

Dim ser As New XmlSerializer(GetType(MyCustomClass))

.... I can create the XmlSerializer fine, but then I can't cast the
deserialized MyCustomClass object I get into a MyInheritsCustomClass object.

Am I missing something? Or is this just not possible for some reason?
 
Looking at the full exception sheds light on the problem.

System.InvalidOperationException: There was an error reflecting type
'Sample.TheirOrg.SEED.User'. ---> System.InvalidOperationException:
Types Sample.MyOrg.SEED.User and Sample.TheirOrg.SEED.User both use
the XML type name, User, from namespace . Use XML attributes to
specify a unique XML name and/or namespace for the type.

As the exception message says, both are using the same Xml type name.
You can remedy this by specifying the Namespace for each using the
XmlRootAttribute(). However, this will not give you the desired
result since the Xml returned from your web service would have to
include the appropriate namespace.

Looking at your code example makes me wonder what it is you are really
trying to accomplish. If you are looking to have a level of
abstraction allowing you to switch between the different User classes,
maybe you should be using an interface.

-KIRBY

Alrighty, here's more complete code...
----------------------------------------
'MyCustomClass
Namespace MyOrg.SEED
<Serializable()> Public Class User
<XmlElementAttribute(ElementName:="Agency", Type:=GetType(_AgencyFull))>
Public Agency() As _AgencyFull
<XmlAttributeAttribute()> Public ID As String
<XmlAttributeAttribute()> Public nID As String
<XmlAttributeAttribute()> Public Name As String
<XmlAttributeAttribute()> Public StartDate As String
<XmlAttributeAttribute()> Public Status As String
<Serializable()> Public Class _AgencyFull
Inherits _Agency
<XmlElementAttribute(ElementName:="Address",
Type:=GetType(_Address))> Public Address As _Address
End Class
End Class


<Serializable()> Public MustInherit Class _Agency
<XmlAttributeAttribute()> Public ID As String
<XmlAttributeAttribute()> Public Name As String
<XmlAttributeAttribute()> Public Primary As Boolean
End Class

<Serializable()> Public Class _Address
<XmlAttributeAttribute(AttributeName:="line1")> Public Line1 As String
<XmlAttributeAttribute(AttributeName:="line2")> Public Line2 As String
<XmlAttributeAttribute(AttributeName:="city")> Public City As String
<XmlAttributeAttribute(AttributeName:="state")> Public State As String
<XmlAttributeAttribute(AttributeName:="postalcode")> Public ZipCode As
String
<XmlAttributeAttribute(AttributeName:="country")> Public Country As
String
End Class

End Namespace


----------------------------------------
'MyInheritsCustomClass
NameSpace TheirOrg.SEED

<Serializable()> Public Class User
Inherits MyOrg.SEED.User
<NonSerialized()> Public XML As String
End Class

End Namespace

'In the application defining the MyInheritsCustomClass
(TheirOrg.SEED.User), I would have a function that retrieves the
deserializable XML from a Web Service, and deserializes it into the derived
class for return...

Public Function LoadUser(ByVal username As String) As TheirOrg.SEED.User
Dim oUser as New TheirOrg.SEED.User
Dim sXML As String = MyWebServiceProxy.GetUser(username)
Dim mynull As XmlParserContext
Dim rXML As XmlReader = New XmlTextReader(sXML, XmlNodeType.Document,
mynull)
Dim xSer As New XmlSerializer(GetType(TheirOrg.SEED.User))
oUser = xSer.Deserialize(rXML)
oUser.XML = sXML
Return oUser
End Function

The XML document looks like this...

<User ID="23" nID="1 " Name="User X"
StartDate="2004-07-19T10:01:13.300" Status="XA">
<Agency ID="56EXD" Name="Organization X" Primary="1"/>
</User>

Forget that the Agency address is not represented in the XML, it's omission
does not affect the deserialization. So, the error I'm getting is in the
line...

Dim xSer As New XmlSerializer(GetType(TheirOrg.SEED.User))

If I change the 'TheirOrg' to 'MyOrg' in the function, and comment out the
"oUser.XML = sXML", the Myorg.SEED.User class is returned just fine. Adding
in the <XmlRoot> clause in both namespaces does not change the error when I
try to initialize the XmlSerializer.

Hope this sheds some more light on the problem.

Kirby Turner said:
The "error in XML document (1,2)" message is caused because the XML
does not match the schema needed to deserialize into
MyInheritsCustomClass. In other words, the root element found at
(1,2) does not match the expected root element. The XML returned from
your web service follows the schema for the class structure
MyCustomClass. To deserialize into an instance of
MyInheritsCustomClass, you must make sure the XML schema for the
MyInheritsCustomClass matches the schema for MyCustomClass. Jakob's
reply has example of this but there is one bug in the example. He
forgot to inherit from MyCustomClass in MyInheritsCustomClass.

public class MyCustomClass
{
// ...
}

[XmlRoot( "MyCustomClass" )]
public class MyInheritsCustomClass : MyCustomClass
{
// ...
}

Please post your class structures so we can see what the problem is if
this still does not work for you.

-KIRBY


I translated your example into VB, but unfortunately, it did not work. I
still had the same "error reflecting type" message. Interestingly, if I
left in the XmlRoot declaration on the MyCustomClass, and tried to
desreialize it instead of the MyInheritsCustomClass, I get an "error in XML
document (1, 2)" error. I take it out, I can deserialize it.



It is possible if you mark your base class and the inherited class with
the
XmlRoot attribute. I.e. (I hope it is ok that I am giving the example in
C#):

[XmlRoot(Namespace = "www.domain.com", ElementName = "MyClassName")]
public class MyInheritsCustomClass
{
// ...
}

[XmlRoot(Namespace = "www.domain.com", ElementName = "MyClassName")]
public class MyCustomClass
{
// ...
}

You can now construct XmlSerializers to serialize and deserialize with the
following code:

// Serialize
XmlSerializer serial = new XmlSerializer(typeof(MyCustomClass));

// Deserialize
XmlSerializer serial = new XmlSerializer(typeof(MyInheritsCustomClass));

Hope, this helps.

Regards, Jakob.


:

I know the subject line is a little confusing, but I always try to use
words
that are searchable for anyone else having a similar problem.

Here's what I've got...

I'm retreiving an XML string from a Web Service that represents a
serialized
custom class [MyCustomClass]. On my client, I have a class that
inherits
from that custom class [MyInheritsCustomClass], and contains just one
new
property. Once I get the XML string from the WebMethod, I want to
Deserialize it into the client's class that inherits from the original
MyCustomClass.

Problems I'm running into...

1) The statement...

Dim ser As New XmlSerializer(GetType(MyInheritsCustomClass))

....gives me an "There was an error reflecting type
'MyInheritsCustomClass'"
exception

If I change this to...

Dim ser As New XmlSerializer(GetType(MyCustomClass))

.... I can create the XmlSerializer fine, but then I can't cast the
deserialized MyCustomClass object I get into a MyInheritsCustomClass
object.

Am I missing something? Or is this just not possible for some reason?
 
Sorry to take so long to reply, but I had to read up in Interfaces to
understand what you were suggesting. If I get you right, you think that I
could create an interface defining the User class (there are going to be
several subclasses involved), and then independently, MyOrg.SEED.User and
TheirOrg.SEED.User can create their class based on that interface?

Even if that wouldn't screw up the serialization, since they would then both
be derived classes, I don't see how that would avoid the error in question.

Am I misunderstanding?


Kirby Turner said:
Looking at the full exception sheds light on the problem.

System.InvalidOperationException: There was an error reflecting type
'Sample.TheirOrg.SEED.User'. ---> System.InvalidOperationException:
Types Sample.MyOrg.SEED.User and Sample.TheirOrg.SEED.User both use
the XML type name, User, from namespace . Use XML attributes to
specify a unique XML name and/or namespace for the type.

As the exception message says, both are using the same Xml type name.
You can remedy this by specifying the Namespace for each using the
XmlRootAttribute(). However, this will not give you the desired
result since the Xml returned from your web service would have to
include the appropriate namespace.

Looking at your code example makes me wonder what it is you are really
trying to accomplish. If you are looking to have a level of
abstraction allowing you to switch between the different User classes,
maybe you should be using an interface.

-KIRBY

Alrighty, here's more complete code...
----------------------------------------
'MyCustomClass
Namespace MyOrg.SEED
<Serializable()> Public Class User
<XmlElementAttribute(ElementName:="Agency", Type:=GetType(_AgencyFull))>
Public Agency() As _AgencyFull
<XmlAttributeAttribute()> Public ID As String
<XmlAttributeAttribute()> Public nID As String
<XmlAttributeAttribute()> Public Name As String
<XmlAttributeAttribute()> Public StartDate As String
<XmlAttributeAttribute()> Public Status As String
<Serializable()> Public Class _AgencyFull
Inherits _Agency
<XmlElementAttribute(ElementName:="Address",
Type:=GetType(_Address))> Public Address As _Address
End Class
End Class


<Serializable()> Public MustInherit Class _Agency
<XmlAttributeAttribute()> Public ID As String
<XmlAttributeAttribute()> Public Name As String
<XmlAttributeAttribute()> Public Primary As Boolean
End Class

<Serializable()> Public Class _Address
<XmlAttributeAttribute(AttributeName:="line1")> Public Line1 As String
<XmlAttributeAttribute(AttributeName:="line2")> Public Line2 As String
<XmlAttributeAttribute(AttributeName:="city")> Public City As String
<XmlAttributeAttribute(AttributeName:="state")> Public State As String
<XmlAttributeAttribute(AttributeName:="postalcode")> Public ZipCode As
String
<XmlAttributeAttribute(AttributeName:="country")> Public Country As
String
End Class

End Namespace


----------------------------------------
'MyInheritsCustomClass
NameSpace TheirOrg.SEED

<Serializable()> Public Class User
Inherits MyOrg.SEED.User
<NonSerialized()> Public XML As String
End Class

End Namespace

'In the application defining the MyInheritsCustomClass
(TheirOrg.SEED.User), I would have a function that retrieves the
deserializable XML from a Web Service, and deserializes it into the derived
class for return...

Public Function LoadUser(ByVal username As String) As TheirOrg.SEED.User
Dim oUser as New TheirOrg.SEED.User
Dim sXML As String = MyWebServiceProxy.GetUser(username)
Dim mynull As XmlParserContext
Dim rXML As XmlReader = New XmlTextReader(sXML, XmlNodeType.Document,
mynull)
Dim xSer As New XmlSerializer(GetType(TheirOrg.SEED.User))
oUser = xSer.Deserialize(rXML)
oUser.XML = sXML
Return oUser
End Function

The XML document looks like this...

<User ID="23" nID="1 " Name="User X"
StartDate="2004-07-19T10:01:13.300" Status="XA">
<Agency ID="56EXD" Name="Organization X" Primary="1"/>
</User>

Forget that the Agency address is not represented in the XML, it's omission
does not affect the deserialization. So, the error I'm getting is in the
line...

Dim xSer As New XmlSerializer(GetType(TheirOrg.SEED.User))

If I change the 'TheirOrg' to 'MyOrg' in the function, and comment out the
"oUser.XML = sXML", the Myorg.SEED.User class is returned just fine. Adding
in the <XmlRoot> clause in both namespaces does not change the error when I
try to initialize the XmlSerializer.

Hope this sheds some more light on the problem.

Kirby Turner said:
The "error in XML document (1,2)" message is caused because the XML
does not match the schema needed to deserialize into
MyInheritsCustomClass. In other words, the root element found at
(1,2) does not match the expected root element. The XML returned from
your web service follows the schema for the class structure
MyCustomClass. To deserialize into an instance of
MyInheritsCustomClass, you must make sure the XML schema for the
MyInheritsCustomClass matches the schema for MyCustomClass. Jakob's
reply has example of this but there is one bug in the example. He
forgot to inherit from MyCustomClass in MyInheritsCustomClass.

public class MyCustomClass
{
// ...
}

[XmlRoot( "MyCustomClass" )]
public class MyInheritsCustomClass : MyCustomClass
{
// ...
}

Please post your class structures so we can see what the problem is if
this still does not work for you.

-KIRBY


I translated your example into VB, but unfortunately, it did not work. I
still had the same "error reflecting type" message. Interestingly, if I
left in the XmlRoot declaration on the MyCustomClass, and tried to
desreialize it instead of the MyInheritsCustomClass, I get an "error
in
XML
document (1, 2)" error. I take it out, I can deserialize it.



It is possible if you mark your base class and the inherited class with
the
XmlRoot attribute. I.e. (I hope it is ok that I am giving the
example
in
C#):

[XmlRoot(Namespace = "www.domain.com", ElementName = "MyClassName")]
public class MyInheritsCustomClass
{
// ...
}

[XmlRoot(Namespace = "www.domain.com", ElementName = "MyClassName")]
public class MyCustomClass
{
// ...
}

You can now construct XmlSerializers to serialize and deserialize
with
the
following code:

// Serialize
XmlSerializer serial = new XmlSerializer(typeof(MyCustomClass));

// Deserialize
XmlSerializer serial = new XmlSerializer(typeof(MyInheritsCustomClass));

Hope, this helps.

Regards, Jakob.


:

I know the subject line is a little confusing, but I always try to use
words
that are searchable for anyone else having a similar problem.

Here's what I've got...

I'm retreiving an XML string from a Web Service that represents a
serialized
custom class [MyCustomClass]. On my client, I have a class that
inherits
from that custom class [MyInheritsCustomClass], and contains just one
new
property. Once I get the XML string from the WebMethod, I want to
Deserialize it into the client's class that inherits from the original
MyCustomClass.

Problems I'm running into...

1) The statement...

Dim ser As New XmlSerializer(GetType(MyInheritsCustomClass))

....gives me an "There was an error reflecting type
'MyInheritsCustomClass'"
exception

If I change this to...

Dim ser As New XmlSerializer(GetType(MyCustomClass))

.... I can create the XmlSerializer fine, but then I can't cast the
deserialized MyCustomClass object I get into a MyInheritsCustomClass
object.

Am I missing something? Or is this just not possible for some reason?
 
Back
Top