我目前被任命为我们的一位客户创建 Active-X 插件。现在我已经在 C#/.NET 中成功创建了 Active-X 插件(一个继承自 System.Windows.Forms.UserControl
类的 Windows 窗体控件),但是托管该插件的应用程序-in 在显示 Active-X 插件的对话窗口的标题中显示控件的类名。
经过大量搜索和反汇编后,我发现主机调用了方法 IOleObject.GetUserType
并且主机使用该方法的返回值作为对话窗口的标题。查看 System.Windows.Forms.UserControl
类,我发现该类继承自 System.Windows.Forms.Control
类,后者又显式实现了 System.Windows.Forms.UnsafeNativeMethods.IOleObject
接口(interface)。
我想知道是否有办法重写 GetUserType
方法
在 UserControl
类中,或者如果有另一种方法可以完成我想要的(也许解决方案非常简单,但到目前为止我还没有看到)。到目前为止,我已经尝试过各种“可能”的解决方案:
我已尝试重新实现
IOleObject
接口(interface),但由于System.Windows.Forms.UnsafeNativeMethods.IOleObject
是内部的,因此不可能为此(您必须使用完全相同的接口(interface),并且重新定义接口(interface)不会导致完全相同的接口(interface))。我已尝试使用 Ziad Elmalki 在 CodeProject 上描述的 CLR 注入(inject).
我尝试过使用某种形式的 AOP。由于
Control
类通过System.ComponentModel.Component
继承自System.MarshalByRefObject
我认为可能让我的用户控件返回一些一种会拦截发送到GetUserType
方法的调用的代理。
不幸的是,我无法让它工作。有效的方法是更改类的名称,但由于类名称不允许包含空格或其他特殊字符,因此这不是一个可接受的解决方案(下划线不一样)。
为了详细说明,这是我想要完成的代码示例(请注意,它并不完整):
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
[ ComVisible(true)
, ClassInterface(ClassInterfaceType.AutoDual)
, Description("My Active-X plug-in")
, Guid("...")
]
public partial class MyControl : UserControl
{
public override int GetUserType(int dwFromOfType, out string userType)
{
userType = "The caption to show in the host";
// Return S_OK
return 0;
}
}
希望这里有人能帮助我。
提前致谢!
最佳答案
您可以尝试实现 ICustomQueryInterface (.Net 4.0):http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.icustomqueryinterface.aspx
当系统查询 IOleObject 时,您可以返回自己实现 IOleObject 的自定义对象,将您不关心的方法转发给 UserControl 实现,并适本地将自定义对象上的 QueryInterface 调用委托(delegate)回 UserControl(聚合).
关于c# - 更改用C#编写的Active-X插件的用户类型名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6276584/