Late Binding in C#

P

Pete

Hi,

I want to essentially perform late binding in c#.

The rough equivalent in VB (6) would be:

dim x as ifcMyInterface
dim SensibleClassName as string

SensibleClassName = .... 'something sensible

set x = createobject(SensibleClassName)

So, I want to read eoungh information to uniquely identify the class
dynamically, but I am expecting it to support a particular interface (or
at the very least to contain a particular method). The class will be
inside a .net library, won't be a com cpt.

Any hints?

This is my first project back in c# after having had to hack stuff in VB
for the last year, and I've still got my COM head on...

TIA



*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
P

Patrick Steele [MVP]

Hi,

I want to essentially perform late binding in c#.

The rough equivalent in VB (6) would be:

dim x as ifcMyInterface
dim SensibleClassName as string

SensibleClassName = .... 'something sensible

set x = createobject(SensibleClassName)

So, I want to read eoungh information to uniquely identify the class
dynamically, but I am expecting it to support a particular interface (or
at the very least to contain a particular method). The class will be
inside a .net library, won't be a com cpt.

Any hints?

To instantiate an object by it's name (as a string) look at
Activator.CreateInstance().
 
G

Guest

No prob, hoped it helped...

If you want to load classes from an assembly, all this is even easier. You can load them, search for a specific class or name, etc.

Sounds like you are trying to implment some form or extensibility. If this is the case, I thought I might offer that I built a system that uses C# scripting. It's really kind of kewl. The user writes code in text. (Actually, one of the apps we have generates code for them, but you get the idea.) This is stored in our database or in some cases a file in a specific directory. At runtime we load the code, run it through the compiler, and then execute it. The first time we run the code it takes a little longer, but not much. Since this is stuff they tend to hammer out and then use over and over, it is really fast. And, it offers the developer (or program) the ability to use our existing C# object model.

The way we implement our interface is through C# attributes. (There is a pre-defined model and documentaiton on how it is supposed to work.) Basically, the designer writes a class. They attach attributes to the class to tell it what methods are what, etc.

A really good example of doing what I described above, and it sounds like what you described, is NUNIT. If you have not seen this, I highly recommend that you pick up a copy. It is a utility to testing .Net software. It is free. And it comes with full source code.

An interesting aspect is that it actually loads a DLL you have built, looks through the assembly and finds classes that are marked with the [TestFixture] attribute. It loads these classes into a GUI. When you tell it to run the tests, it finds all the methods for the classes it found that have the [Test] attribute.

There are way too many things NUnit does to describe it all here. You can get a copy at http://www.nunit.org/.

Hope this helps...

Frisky
 

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

Similar Threads


Top