COM/.Net互操作性:出现名称冲突时,如何从导出的类型中删除名称空间前缀? [英] COM / .Net interoperability: how to remove namespace prefixes from exported types when there is a name conflict?

查看:98
本文介绍了COM/.Net互操作性:出现名称冲突时,如何从导出的类型中删除名称空间前缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want to implement a .Net wrapper around a Com Coclass (defined in a C++ dll), that has the exact same name and interface name as the COM object, but within in a different namespace. When I try to do that, I find that tlbexp adds a prefix to the name of the class (the exported type becomes namespace.namespace_classname). This is annoying because then the tlb file cannot be used easily in VBA for instance.

这里是一个使情况更清楚的示例

Here is an example to make things clearer

C ++代码类似于:

The C++ code is something like:

namespace CPP
{
   
public interface IComObjectTest
   
{ ... }
   
public class ComObjectTest : IComObjectTest
   
{ ... }
}

The C# code is something like:

namespace CS
{
   
public interface IComObjectTest : CPP.IComObjectTest
   
{
         
...
   
}

   
public class ComObjectTest : CS.IComObjectTest
   
{
         
private CPP.ComObjectTest _test;
         
public ComObjectTest(CPP.ComObjectTest test) { _test = test; }

         
public CPP.ComObjectTest GetComObjectTest { get { return _test; } }

         
...
   
}
}

When I compile the C# project and run tlbexp.exe on it. I get the following types exported:

CS.CS_ComObjectTest
CS
.CS_IComObjectTest

while I would like to see:

CS.ComObjectTest
CS
.IComObjectTest

Is this a limitation that can be overcome? Any help appreciated, Thanks

推荐答案

为什么要围绕COM对象围绕.NET包装器生成TLB? VBS与COM一起使用,因此请使用COM对象最初生成的TLB,而不是尝试通过.NET(这要求您将.NET组件创建为COM. 无论如何还是对象).曾经围绕COM对象创建.NET包装器的唯一原因是,您可以在.NET程序中使用COM对象.由于VBS不支持.NET,因此不需要这样的包装器.这完全消除了问题.

为回答您的问题,C#不需要在类型周围使用名称空间,但我们使用名称空间的全部原因是为了避免冲突,并且由于.NET包装器仅设计用于.NET应用程序,因此始终将其包含在.NET应用程序中是有道理的.命名空间.实际上 在.NET代码中,访问全局名称空间中的类型可能有点麻烦,因为您可能必须使用全局运算符.

Why are you generating a TLB around a .NET wrapper around a COM object?  VBS works with COM so use the TLB that the COM object originally generated rather than trying to go through .NET (which would require that you create the .NET component as a COM object anyway).  The only reason you ever create a .NET wrapper around a COM object is so you can use the COM object in a .NET program.  Since VBS doesn't support .NET it doesn't need such a wrapper.  This completely eliminates the problem.  

In answer to your question C# doesn't require a namespace around a type but the entire reason we use namespaces is to avoid collisions and since a .NET wrapper is designed for use only in .NET apps it makes sense to always have it in a namespace.  In fact accessing types that are in the global namespace can be a little uglier in .NET code because you might have to use the global operator.

Michael Taylor-2011年8月29日
http://msmvps.com/blogs/p3net

Michael Taylor - 8/29/2011
http://msmvps.com/blogs/p3net


这篇关于COM/.Net互操作性:出现名称冲突时,如何从导出的类型中删除名称空间前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆