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

Casting as Base Class on Deserialize ?

P: n/a
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

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

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:


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

Any help much appreciated,

Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.