如何从 COM 中隐藏 GetType() 方法?
我为 Excel 制作了一个自动化插件,并且制作了几个函数(公式)。
我有一个类,其标头如下所示(它是 COM 可见的):
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class Functions
{}
在方法列表中,我看到:
ToString(), Equals(), GetHashCode() and GetType() methods.
由于我的类的所有方法都是 COM 可见的,我应该以某种方式隐藏这 4 个方法。 我成功使用了其中的 3 个:
ToString(), Equals(), GetHashCode()
但 GetType() 无法被覆盖。
以下是我对其中 3 个所做的操作:
[ComVisible(false)]
public override string ToString()
{
return base.ToString();
}
[ComVisible(false)]
public override bool Equals(object obj)
{
return base.Equals(obj);
}
[ComVisible(false)]
public override int GetHashCode()
{
return base.GetHashCode();
}
这不起作用:
[ComVisible(false)]
public override Type GetType()
{
return base.GetType();
}
这是编译时 Visual Studio 中的错误消息:
..GetType()': cannot override inherited member 'object.GetType()' because it is not marked virtual, abstract, or override
那么,我应该怎样做才能从 COM 中隐藏 GetType() 方法?
I made an automation Add-In for Excel, and I made several functions (formulas).
I have a class which header looks like this (it is COM visible):
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class Functions
{}
In a list of methods, I see:
ToString(), Equals(), GetHashCode() and GetType() methods.
Since all methods of my class are COM visible, I should somehow hide those 4.
I succeeded with 3 of them:
ToString(), Equals(), GetHashCode()
but GetType() cannot be overriden.
Here is what I did with 3 of them:
[ComVisible(false)]
public override string ToString()
{
return base.ToString();
}
[ComVisible(false)]
public override bool Equals(object obj)
{
return base.Equals(obj);
}
[ComVisible(false)]
public override int GetHashCode()
{
return base.GetHashCode();
}
This doesn't work:
[ComVisible(false)]
public override Type GetType()
{
return base.GetType();
}
Here is the error message in Visual Studio when compile:
..GetType()': cannot override inherited member 'object.GetType()' because it is not marked virtual, abstract, or override
So, what should I do to hide the GetType() method from COM?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该引入一个新的 COM 接口,并使用
ClassInterfaceType.None
继承您的类。这样您将只公开该接口中的方法。You should introduce a new COM interface and inherit your class from it with
ClassInterfaceType.None
. This way you will only expose the methods in that interface.我认为 COM 使用接口并隐藏实现,所以我会这样做。据我所知,没有办法隐藏 GetType。
I thought COM used Interfaces and hides the implementation, so i would go with that. There is no way to hide GetType as fas as i know.