473,386 Members | 1,764 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,386 software developers and data experts.

Runtime Instance Serialisation of Classes only known at Runtime themselves.

I have an classic object model, base class with two or moresubclasses. When I know exactly that there is only these twoclasses then I have no problem serilaising and deserialsing theinstances. However, my new requirement concerns a pluginarchitecture that loads subclasses that are only known atruntime. The first hurdle of reading these differing subclassesfrom a configuration file was relatively trivial, although Ithought it would be my biggest are of concern. However, theserialising of these instances is proving to be a headache (andI thought it would be the easier part). Basic structure is aclass containing an ArrayList that, in turn, contains each ofthe instances of the subclass.

Given the ParkingLot example from TopXML (a superb XML IMHO) mychanges are:

private static ParkingLot SetupParkingLot()
{
ParkingLot lot = new ParkingLot();
lot.Vehicles = new ArrayList();
Car wifesCar = new Car();
wifesCar.Make = "Ford";
wifesCar.Model = "Explorer";
wifesCar.Year = 1997;
wifesCar.VIN = "ABC123DEF";
Car myCar = new Car();
myCar.Make = "Mercedes Benz";
myCar.Model = "SLK500";
myCar.Year = 2005;
myCar.VIN = "ABC123DEF";
Motorcycle neighborsToy = new Motorcycle();
neighborsToy.Make = "Ducati";
neighborsToy.Model = "Monster";
neighborsToy.Year = 1995;
neighborsToy.VeryLoud = true;

lot.Vehicles.Add( wifesCar );
lot.Vehicles.Add( myCar );
lot.Vehicles.Add( neighborsToy );

return lot;
}
and

private static void SerializeCustomParkingLot(XmlWriterwriter,
ParkingLot parkingLot)
{
XmlAttributes carsAttributes = new XmlAttributes();
XmlAttributes classAttributes =
new XmlAttributes();

classAttributes.XmlRoot =
new XmlRootAttribute("ParkingLotRoot");

carsAttributes.XmlArrayItems.Add( new
XmlArrayItemAttribute("ACar", typeof(Car)));
carsAttributes.XmlArrayItems.Add( new
XmlArrayItemAttribute("AMotorcycle", typeof(Motorcycle)));

XmlAttributeOverrides overrides =
new XmlAttributeOverrides();

overrides.Add(typeof(ParkingLot), classAttributes);
overrides.Add(typeof(ParkingLot), "Cars", carsAttributes);

try
{
XmlSerializer xs = new XmlSerializer(
typeof(ParkingLot), overrides );

xs.Serialize( writer, parkingLot );
}
catch( InvalidOperationException ex )
{
System.Console.WriteLine( "Bad override attributes" );
}
}

Trouble is I will always receive the InvalidOperationExceptionwith an inner exception of

The type Car was not expected. Use the XmlInclude or SoapIncludeattribute to specify types that are not known statically.

Could anybody please shed some light on this?

--------------------------------
From: Graham McDonald

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>4rOzxcdD1UWXcqK03uMyOw==</Id>
Nov 12 '05 #1
0 886

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

Similar topics

7
by: Carlos Ribeiro | last post by:
I'm looking for ways to load new class definitions at runtime (I'm not talking about object instances here, so a persistent object database isn't what I am looking for ). I'm aware of a few...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
18
by: Microsoft | last post by:
When I try this in my code I alwas get an errormessage: "Object reference not set to an instance of an object" Dim g As System.Drawing.Graphics g.DrawString("Test", New Font("Arial", 12,...
2
by: Greg | last post by:
I have a bizarre situation in which serialisation is failing routinely under a specific condition, and I'm wondering if the details ring a bell with anyone here. I have 2 classes that my...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
9
by: Mike | last post by:
I was messing around with adding methods to a class instance at runtime and saw the usual code one finds online for this. All the examples I saw say, of course, to make sure that for your method...
3
by: Rotsey | last post by:
Hi, I am trying to send a object to a webservice and I get an serialisation error on the object saying "object not expected" add "XmlInclude or SoapInclude attribute" I have done this but...
10
by: Sami | last post by:
Hi, Is there anyway to construct a object of class with its class name obtained dynamically? Prototype and Factory method along with Abstract factory design pattern can be used to do it. But it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.