Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am currently working with COM objects in managed code and am using the new dynamic type for this. This works well in some areas but can be an issue in others.

I was think about how I could get the best of both worlds, the flexibility of the dynamic type (late bound) with the support for say, an RCW (early bound)

Somehow wrapping the dynamic type in a more manageable stucture. I was wondering if there was a preferred method for this (if it is even a good idea) or what things I should consider.

The two basic ideas I came up with so far as follows:

Firstly, creating a static class that allows me to call the methods of the dynamic type in a managed way.

public static class ComObjectWrapper
{
   public static void SomeMethod(dynamic comObject, int x)
   {
      comObject.someMethod(x);
   }

   public static bool GetSomeProp(dynamic comObject)
   {
      comObject.getSomeProp();
   }

   public static void SetSomeProp(dynamic comObject, bool foo)
   {
      comObject.setSomeProp(foo);
   }
}

Secondly, creating a class that is constructed using the com object, then mapping all its members to managed properties, methods, etc.

public class ComObjectWrapper
{
   private dynamic comObject = null;

   public ComObjectWrapper(dynamic comObject)
   {
     this.comObject = comObject;
   }

   public void SomeMethod(int x)
   {
      comObject.someMethod(x);
   }

   public bool SomeProp
   {
      get
      {
         return comObject.getSomeProp();
      }
      set
      {
         comObject.setSomeProp(value);
      }
   }
}

Are there other ways to approach this? Am I missing something stupid!?

share|improve this question
If you have the option for an RCW at all then using dynamic makes no sense. Almost all COM servers support it, import the type library. – Hans Passant Feb 23 '11 at 20:33
Sorry I don't agree at all - for example; if the type library, which is outside my control, is updated then any application based on its RCW is broken. Secondly, what about all the versions of the type library, I can only target one version. Essentially I want to late-bind to avoid version dependence on a library I have no control over. – Fraser Feb 23 '11 at 21:18

1 Answer

up vote 2 down vote accepted

The opensource framework Impromptu-Interface will wrap a dynamic object with a static interface such that all the statically defined members from the interface use the dlr to forward to the dynamic object.

Create Your interface

IComObjectWrapper
{
   void SomeMethod(int x);
   bool SomeProp;
}

Then where you need to wrap your com object include ImpromptuInterface

  using ImpromptuInterface;

And finally to wrap it:

var tStaticTyped = Impromptu.ActLike<IComObjectWrapper>(comObject);
share|improve this answer
This sounds interesting - I will give it a go now! – Fraser Feb 28 '11 at 16:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.