将多个类似的com对象组合在单个托管包装器中
我正在编写在COM服务器中加载和执行的托管应用程序(使用.NET)。在应用程序中,我必须与多个COM接口/对象(由COM服务器提供)进行交互。这些COM接口共享一组具有相同签名和行为的方法,唯一的区别是内部上下文。例如,Interop库中的COM接口定义看起来像:
interface IComInterface1 {
Object GetObject(String str, Int32 someInt);
// more methods are defined
}
interface IComInterface2 {
Object GetObject(String str, Int32 someInt);
// more methods are defined
}
这些方法有些原始,需要其他编码(例如分配/释放资源,确保方法调用序列等),并且对于两个COM接口,它们的处理都是相同的。
我的目标是编写一个单个和统一的托管(在C#)对象,该对象将隐藏内部COM接口处理并仅对应用程序方法有用。并消除对托管类的重复实施。
我已经将这个目标分为两个任务:
- 创建托管类,该类在私有字段中保留每个共享方法的方法
- 添加公共方法,这些方法可以揭示业务逻辑的有用功能,并将隐藏特定于com的方法处理(分配/释放资源,请确保方法调用序列等。)
接口方法处理在两种情况下都是相同的。这是我所包含的:
class MyManagedWrapper {
readonly Func<String, Int32, Object> _getObject;
<...> // additional funcs for every shared method
public MyManagedWrapper(IComInterface1 comObj) {
_getObject = comObj.GetObject;
// assign more methods to funcs
}
public MyManagedWrapper(IComInterface2 comObj) {
_getObject = comObj.GetObject;
// assign more methods to funcs
}
public String GetUsefulData() {
// here I no longer care about which interface I'm using, because
// I'm going to call only funcs, hide COM management routine
// and produce return value which is relevant for business logic
}
}
此对象的寿命等于COM服务器的寿命。
我相信这是一个有效的解决方案,我的主要关注点是Func代表Invocation绩效开销。我的应用程序运行的COM服务器有些关键性,我必须确保我添加最低可能的性能开销。也就是说,我无法在运行时使用反射(明确或隐式)。
我有两个问题:
func
调用添加额外的性能惩罚与com对象的直接方法调用相比吗?- 我是否需要保留传递的com对象引用(将其方法分配给构造函数中的委托)以避免在运行时收集垃圾?
I'm writing a managed application (using .NET) which is loaded and executed in COM server. Inside the application, I have to interact with several COM interfaces/objects (provided by COM server). These COM interfaces share a set of methods with identical signature and behavior, the only difference is internal context. For example, COM interface definition from interop library would look like:
interface IComInterface1 {
Object GetObject(String str, Int32 someInt);
// more methods are defined
}
interface IComInterface2 {
Object GetObject(String str, Int32 someInt);
// more methods are defined
}
These methods are somewhat raw and require additional coding (like allocating/releasing resources, ensure method call sequence, etc.) and their processing is identical for both COM interfaces.
My goal is to write a single and unified managed (in C#) object which will hide internal COM interface handling and expose only useful for application methods. And eliminate duplicate implementation of managed classes.
I've split this objective into two tasks:
- Create managed class that keeps methods for each shared method in private fields
- Add public methods that expose useful functionality for business logic and will hide COM-specific method handling (allocating/releasing resources, ensure method call sequence, etc.)
Interface method processing is identical in both cases. Here is what I come with:
class MyManagedWrapper {
readonly Func<String, Int32, Object> _getObject;
<...> // additional funcs for every shared method
public MyManagedWrapper(IComInterface1 comObj) {
_getObject = comObj.GetObject;
// assign more methods to funcs
}
public MyManagedWrapper(IComInterface2 comObj) {
_getObject = comObj.GetObject;
// assign more methods to funcs
}
public String GetUsefulData() {
// here I no longer care about which interface I'm using, because
// I'm going to call only funcs, hide COM management routine
// and produce return value which is relevant for business logic
}
}
This object's lifetime equals to COM server lifetime.
I believe this is a working solution and my primary concern is Func delegate invocation performance overhead. The COM server my application runs in is somewhat performance-critical and I have to ensure that I'm adding minimum possible performance overhead. That is, I can't use reflection (explicitly or implicitly) at runtime.
And I have two questions:
- Does
Func
invocation add extra performance penalty comparing to direct method invocation on COM objects? - Do I need to keep the passed COM object reference (after assigning its methods to delegates in constructor) to avoid it from being garbage collected at runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论