473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Serializing of inherited Classes

Hello.

I'm trying to serialize an inherited Class by a Webservice. Here ist a
Code-Snippet of the Classes I try to serialize:

// Base-Class
public class Test
{
private string _Argument1;
public string Argument1
{
get { return _Argument1; }
set { _Argument1 = value; }
}
}

// Inherited Class
public class TestEx : Test
{
private string _Argument2;
public string Argument2
{
get { return _Argument2; }
set { _Argument2 = value; }
}
}

Now I try to return the Base-Class by a WebService:

[WebMethod]
[XmlInclude(typeof(Test))]
public Test ReturnTest()
{
TestEx testEx = new TestEx();
testEx.Argument1 = "Arg1";
testEx.Argument2 = "Arg2";

return (Test) testEx;
}

But this code throws an Exception: The Type inheritanceText.TextEx was
expected.

If I change the XMLInclude-Attribute to [XmlInclude(typeof(TestEx))], no
Exception is thrown, but the Webservice returns TestEx-Object. But I want the
Base-Class returned ...

What am I doing wrong?

Thanks in advance,

Björn Schwemmer

--
iucon GmbH
Software Developement
http://www.iucon.de
Nov 12 '05 #1
6 2512
Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when you specify the base class to
XmlIncludeAttribute, an exception was thrown. If there is any
misunderstanding, please feel free to let me know.

According to the XmlIncludeAttribute document, when applying the
XmlIncludeAttribute, we have to specify the Type of the derived class. When
the XmlSerializer serializes objects that include both the base and the
derived class, it can then recognize both object types.

This is by design. So in this case, we have to specify the actual object
type and do the cast when you get it at the client side.

Please check the following link for more information.

http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemxmlserializationxmlincludeattributeclas stopic.asp

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #2
Hi.

Thanks for your fast reply.

You understand my issue right. I want to return my base class at the
WebService-Method: "public Test ReturnTest()". Due to security reasons, I
dont want to pass Arguments from the derived class (TestEx) to the client.
Thats why I tried to cast TextEx back to its base class: "return (Test)
testEx;" at server side.

Is there any chance to cast the derived class (TestEx) to the base class
(Test) at server side and pass it to the client?

Thanks in advance,

Björn Schwemmer

--
iucon GmbH
Software Developement
http://www.iucon.de
Nov 12 '05 #3
Hi,

As far as I know, we cannot return the object as base class automatically.
Because the web service will serialize the object as the base class, so
that exceptions are generated. To cut all the derived class members, the
only way I can see is to construct a new base class object and assign
values to it. Then return the new object client.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #4

--
iucon GmbH
Software Developement
http://www.iucon.de
"Kevin Yu [MSFT]" wrote:
Hi,

As far as I know, we cannot return the object as base class automatically.
Because the web service will serialize the object as the base class, so
that exceptions are generated. To cut all the derived class members, the
only way I can see is to construct a new base class object and assign
values to it. Then return the new object client.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #5
Thanks for your answer.

Then I will create a new Object and copy the Attributes.

Regards,

Björn Schwemmer

Nov 12 '05 #6
You're welcome.

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeff Bunting | last post by:
I'm trying to serialize a class I have and have been getting: An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll There was an error reflecting type...
2
by: Aleksei Guzev | last post by:
Imagine one writing a class library CL1 for data storage. He defines classes ‘DataItem’ and ‘DataRecord’ so that the latter contains a collection of the former. And he derives class ‘IntItem’ from...
1
by: Ivo Bronsveld | last post by:
All, I have quite a challenging task ahead of me. I need to write an object model (for code access) based on a schema, which cannot be made into a dataset because of it's complexity. So I...
2
by: Tobias Zimmergren | last post by:
Hi, just wondering what serializing really is, and howto use it? Thanks. Tobias __________________________________________________________________ Tobias ICQ#: 55986339 Current ICQ status: +...
4
by: Dan | last post by:
I have a need to make a set of classes that all share the same public methods, some implementation and some data. So, I made an abstract base (BaseClass) with an interface (IBaseClass) and a...
2
by: Simon | last post by:
I'm developing a new application and want to use serialization as a way to save my data. But as I add new variables to my classes, how will serializing cope with that? For example, suppose I have...
0
by: j.askey | last post by:
Hi, I have a class that is inherited from Panel. Because Panel is not serializable, I have implemented the ISerializable interface on this class and I can sucessfully save and load classes from...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
12
by: Janaka Perera | last post by:
Hi All, We have done a object oriented design for a system which will create a class multiply inherited by around 1000 small and medium sized classes. I would be greatful if you can help me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.