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

Casting as Base Class on Deserialize ?

I am creating an application that allows the user to link a plug-in
"utility" class (Watcher) to a class at runtime. There are several
Watcher utilities, each with a different style of "watching"
something. The all inherit from BaseWatcher which implements an
IWatcher interface. To configure the Watcher we have a configuration
object per Watcher called <watcher_name>WatcherConfig inheriting from
BaseWatcherConfig which implements IWatcherConfig.

Each custom Watcher is implemented in a separate Assembly dll under
the xxxxx.xxxxx.Watcher namespace.

BaseWatcherConfig and all the Interfaces are available to the main
application.

Because the main application does not know which Watcher will be used
the references are stored as IWatcher and IWatcherConfig.

I want to reconstitute the serialised <watcher_name>WatcherConfig
object file as a BaseWatcherConfig and then use it to generate a
Watcher via the WatcherFactory, which accepts IWatcherConfig as a
building block

The <watcher_name>WatcherConfig is unknown to the main application
until after the dll is loaded with Assembly.LoadFile().

So I have a SOAP formatted <watcher_name>WatcherConfig object map
serialised to disk which I want to reconstitute and as a
BaseWatcherConfig. I am using:

IWatcher _watcher = (IWatcher)watcherFactory.GetWatcher((IWatcherConfi g)BaseWatcherConfig.Deserialize(_watcherConfigFile .FullName));

The deserialize routine:

...
SoapFormatter formatter = new SoapFormatter();
BaseWatcherConfig liveObject =
(BaseWatcherConfig)formatter.Deserialize(fileStrea m);
...

The exception thrown when executing the Deserialze line is:

"Parse Error, no assembly associated with Xml key
a1:http://schemas.microsoft.com/clr/nsa...eyToken%3Dnull
FolderWatcherConfig"

So..I take it I can't do this, so the question is... If I load all
available Watcher assemblies from disk at application load that will
then allow me to cast the serialised object corectly:

<watcher_name>WatcherConfig liveObject =
(<watcher_name>WatcherConfig)formatter.Deserialize (fileStream);

So how, short of a HUGE switch statement (which defeats the point),
can I make this cast selection dynamic.. I have the name of the custom
Watcher so I can generate a string with the Type name like:

watcherName+"Config"

How has I get this into the cast with any elegance ?

Any help much appreciated,

Al
Nov 13 '05 #1
1 4795
I resolved the problem by moving the Deserialise() method into the
IWatcher/BaseWatcher/<watcher_nane>Watcher class releationship. Each
specific <watcher_nane>Watcher class has a private Deserialise()
method that perfoms a specific cast against the object map file.

I instantiate a basic <watcher_nane>Watcher class using a
WatcherFactory, which locates the assembly dll and then loads an
instance of the class. From here its a question of passing the classes
public Configure() method the full path to the serialised object map
and bingo.

All works fine now.

Now I'm having a problem with the Events raised by the new
<watcher_nane>Watcher only being handled by the Base Class of the
Class that has subscribed to them. Very odd.. although I suspect its
to do with the fact that the receiving object is stored locally as an
Interface reference instead of its actual Type.
Nov 13 '05 #2

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

Similar topics

3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
0
by: Kurt Lange | last post by:
no... the array is created dynamically. and no... that defeats the purpose of what im trying todo.. encapsulate all initializing of variables in base class... derive from it... by deriving...
2
by: Andrew G. J. Fung | last post by:
Can anyone please explain to me why the code below throws an exception? It seems to occur when deserializing an instance of a subclass, whose base class holds a struct, where said struct holds an...
61
by: Ken Allen | last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years. One of the drawbacks with VB6 and earlier was the difficulty in casting a 'record' to a different 'shape' so one could...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
1
by: Peter Nofelt | last post by:
Hey All, I am having issue with serializing a class and its base class using ..net 2.0. I am using IXmlSerializable I've provided code displaying my at the bottom of this post. Thanks ahead...
9
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I...
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.