design patterns - Wrapping a COM object/dynamic type in C# -


i working com objects in managed code , using new dynamic type this. works in areas can issue in others.

i think how best of both worlds, flexibility of dynamic type (late bound) support say, rcw (early bound)

somehow wrapping dynamic type in more manageable stucture. wondering if there preferred method (if idea) or things should consider.

the 2 basic ideas came far follows:

firstly, creating static class allows me call methods of dynamic type in 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 class constructed using com object, mapping members 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    {             {          return comobject.getsomeprop();       }       set       {          comobject.setsomeprop(value);       }    } } 

are there other ways approach this? missing stupid!?

the opensource framework impromptu-interface wrap dynamic object static interface such statically defined members interface use dlr forward dynamic object.

create interface

icomobjectwrapper {    void somemethod(int x);    bool someprop; } 

then need wrap com object include impromptuinterface

  using impromptuinterface; 

and wrap it:

var tstatictyped = impromptu.actlike<icomobjectwrapper>(comobject); 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -