在C#中包装COM对象/动态类型 [英] Wrapping a COM object/dynamic type in C#
问题描述
我目前使用COM对象在托管代码,我使用新的动态类型为此。这在某些方面效果很好,但在其他方面可能是一个问题。
我想到了如何获得最好的两个世界,动态类型的灵活性后期绑定),支持RCW(早期绑定)
不知何故将动态类型包装在更易于管理的结构中。我想知道是否有一个首选的方法(如果它甚至是一个好主意)或什么事情我应该考虑。
我想出了两个基本的想法如下:
首先,创建一个静态类,允许我以管理的方式调用动态类型的方法。
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);
}
}
其次,创建一个使用com对象,然后将其所有成员映射到托管属性,方法等。
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);
}
}
}
这个?我
开源框架 Impromptu-Interface 将使用静态接口包装动态对象,以使来自接口的所有静态定义的成员使用dlr转发到动态对象。 / p>
创建您的界面
IComObjectWrapper
{
void SomeMethod(int x);
bool SomeProp;
}
那么你需要包装你的com对象包括ImpromptuInterface
使用ImpromptuInterface;
最后换行:
var tStaticTyped = Impromptu.ActLike< IComObjectWrapper>(comObject);
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!?
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);
这篇关于在C#中包装COM对象/动态类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!