2

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!?

2
  • 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. Commented Feb 23, 2011 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. Commented Feb 23, 2011 at 21:18

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

This sounds interesting - I will give it a go now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.