tags:

views:

48

answers:

2

I am using an 3rd party assembly that provides services via a static class:

Foo.Bar.StaticLibraryClass.Start();
var x = Foo.Bar.StaticLibraryClass.GetSomeStuff();
Foo.Bar.StaticLibraryClass.Stop();

If the class were not static, I could use an instance of it via a dynamically typed variable:

dynamic lib = new Foo.Bar.NotStaticLibraryClass();
lib.Start();
var x = lib.GetSomeStuff();
lib.Stop();

Unfortunately however, the class IS static. Is there any equivalent I can write that will let me work in the same way?

dynamic lib = /* ??????? */
lib.Start();
var x = lib.GetSomeStuff();
lib.Stop();

(Why do I want to use a dynamic variable to access a perfectly good .NET type? There are actually multiple versions of the library DLL, and I must identify and load the appropriate one at runtime. The different DLLs expose the same type and method names, but they do inherit from any common interface. So if I can find a way to use dynamic typing, it will spare me from writing a lot of fiddly reflection code to use the DLLs' methods.)

A: 

c#/vb.net etc are statically typed languages. Hence, if you have a type that doesn't have Start method, the code won't compile.

dynamic keyword bridges the gap between static languages and dynamic languages.
i.e whether or not a method exists, is checked at runtime in case of dynamic languages.

From your example, the Start, Stop, 'GetSomeStuffmethod exists and your code is aware of it. Moreover,dynamic` has got nothing to do with loading an assembly dynamically.

I think you need some kind of plugin/provider kind of a model whereby, you will have a base interface to which your implementation class(es) will confirm.

e.g.

interface IService
{
   void Start();
   void Stop();
   int GetSomeStuff();
}

And the implementation classes (which will be in a separate assembly) will confirm to this interface, which you will load dynamically & cast to this interface (IService)in your code.

Dependency Injection/Inversion of Control is a concept that comes to my mind, which I think should serve what you are looking for.

shahkalpesh
You are correct that the primary use of the dynamic keyword is to bridge from static to dynamic languges or to COM objects. However it is not restricted to that use. For example, I can write the following and it works fine: `dynamic st1 = "test"; dynamic st2 = st1.ToUpper(); string st3 = st2.ToString();` I happen to have a use case where I need to load varying versions of an assembly from which I cannot demand any common interface, even though they have similar type names internally. Using `dynamic` will allow me to avoid lots of fiddly manual reflection.
Eric
@Eric: `dynamic` is useful with types that are already loaded (I guess). Hence, loading an assembly has to be done manually.Creating an instance of the type (using `Activator.CreateInstance`) and assigning that to a `dynamic` variable should help in that case.
shahkalpesh
It's a static type, so `Activator.CreateInstance` should not work...
Eric
A: 

Sorry, C# dynamic does not support this usage directly. In order to call a class method in C#, you have to name the type in your source code directly. And to do that, you have to reference the assembly. dynamic doesn't change this.

dynamic does dispatch to class methods when necessary (such as when you have a dynamic argument), but even in those cases the limitation above stands.

If the target types provided an instance that you could put in a dynamic variable, then you'd be fine. If you provided such an instance you'd be fine. Alternatively, if you have your heart set on dynamic, you could write your own IDynamicMetaObjectProvider that wraps these class methods dynamically given a System.Type. That would be an instructive project if you want to learn about the DLR, at least.

Chris Burrows