Casting as Base Class on Deserialize ?

A

Alex

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((IWatcherConfig)BaseWatcherConfig.Deserialize(_watcherConfigFile.FullName));

The deserialize routine:

...
SoapFormatter formatter = new SoapFormatter();
BaseWatcherConfig liveObject =
(BaseWatcherConfig)formatter.Deserialize(fileStream);
...

The exception thrown when executing the Deserialze line is:

"Parse Error, no assembly associated with Xml key
a1:http://schemas.microsoft.com/clr/ns...6.38412, Culture=neutral, PublicKeyToken=null
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
 
A

Alex

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top