Find the right library at runtime

Hello,

Im writing a program that checks several company's websites for prices and compares them. Since every website is different, i have a different library with specific methods for each website. I want to be able to add libraries later on without having to change the source of my program.

What i wanted to do was specify the library names in xml, use that to make an arraylist of websites, and use reflection to use those libraries. However, the program can't find the libraries unless i add the right reference before compiling.

Is it possible to add libraries and use them without having to add a reference and recompiling my program?

Thanks in advance,

Stijn

[679 byte] By [StijnVanHamme] at [2007-12-25]
# 1

Absolutly it is! ... and kinda fun too (I love reflection).

In the simplest sense... lets say that right off the bad you define two assemblies, your main app and an assembly containing a common interface that all other worker classes will reference and implement. The same goes for your main app, it too will reference this interface class so that it knows how to talk to one of these objects.

Next, you’d load the assembly into your application.

Afterwards you’d create an instance of the type within the assembly and typecast the return value to the type of your Interface.

From there, you need only make calls to your interface reference just as you would if you had declared an instance like so:

IMyType mt = new MyTypeClass();

Does this make sense and point you in the right direction?

BrendanGrant at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
Yes, using Activator.CreateInstanceFrom. You can create instance of object that is defined in some assembly file. Then you can use his events and methods. If you like that you use that instance in typed manner you can cast that object in some already created object that you have loaded, so only the behavior of that object will be difererent. Of course that object should be inherited from the object that you have already in project but will have different implementation of events and methods. This can't be explained that simple, but you can find many articles on the web how to create instance of object defined in some dll file using Activator.CreateInstanceFrom(filepath, ...).
boban.s at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
Thanks alot both of you for the quick response. Now i know in which direction to look and im sure ill solve my problem.
StijnVanHamme at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 4
I still haven't gotten this completely right...

Right now i got 3 assemblies - the main program, the library, and the interface (which is a library aswell). This is what they look like (simplified):

The interface:

public interface IMyInterface
{
string TestMethod();
}

The library has a reference to the interface and looks like this:

public class SomeClass : IMyInterface
{
public string TestMethod() { return "test";}
}

The main program also has a reference to the interface and contains:
...
IMyInterface someInstance;
Assembly asm = Assembly.Load(assemblyName); //no problem here
someInstance = (IMyInterface)asm.CreateInstance(className);
--> this is where i get a InvalidCastException, even though they are referencing and using the same interface. Without casting (just creating an object) there is no problem.

I found a few articles where someone does the exact same thing and it works. I dont have a clue what the problem could be.

StijnVanHamme at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 5
Guessing a bit: the cast is casting the 'asm' reference. You need parentheses:
someInstance = (IMyInterface)(asm.CreateInstance(className));
nobugz at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...