abstract class, implementing virtual class loaded at runtime

R

Robb Sadler

I am trying to write several hardware interfaces that
would use the same base class and functions and be
implemented differently. I don't want to ship all of the
interfaces, but want to access them using the same generic
base class.

I have seen that I can use the Assembly.load method to
load the implementation I need for the hardware being
connected to, but wish to write the code such that I can
reference the methods of the class directly, and take
advantage of strong typing, etc. I don't see where using
dynamic loading of an assembly I can subsequently hook up
my strongly typed code to that assembly code.

Can I create this class from the loaded assembly and then
reference it using only the abstract class (eliminating
the requirement for the main compilation to know anything
about the derived class)?

The problem I am running into is how to allow the app to
switch between derived classes, without them all being
compiled in. Even the problem of instantiating them
correctly is befuddling me.

Here is an example:

namespace test
{
/// <summary>
/// base class for HW ctrl
/// </summary>
public class HWCtrl
{
public HWCtrl()
{

}

public virtual bool Run()
{
return true;
}
}
}

Now in a separate assembly:

namespace test
{
/// <summary>
/// derived class for HW ctrl
/// </summary>
public class DerivedHW : HWCtrl
{
public HWCtrl()
{

}

public bool Run()
{
return true;
}
}
}

I want to drop in the proper assembly for the job, and
bind to it at run time. Maybe this is easy, maybe it's not
possible, but I can't seem to find the answer in the
libraries or the web.

I have thought about using identical GUIDs for all the HW
implementations and creating a class interface, but that
seems to create its own problems.

Anyone been here before?

Thanks in advance,

Robb Sadler
 
J

Jon Skeet [C# MVP]

I want to drop in the proper assembly for the job, and
bind to it at run time. Maybe this is easy, maybe it's not
possible, but I can't seem to find the answer in the
libraries or the web.

I *think* your question is answered in
http://www.pobox.com/~skeet/csharp/plugin.html
- which tries to answer a different question, but answers yours on the
way anyway. Try going through the example and see whether that does
what you need it to.
 
R

Robb Sadler

Thanks Jon!

That was exactly what I was looking for. I was pretty sure
it could be done and looked in the MSDN for hours trying
to find the proper stuff to implement it. If I had known
to look for the keyword "CreateInstance" I would have been
all set as I found out with a quick search after the fact.

Many thanks, I have what I need.

Best regards,

Robb
 

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