【问题标题】:Overloading a method in com visible dll重载 com 可见 dll 中的方法
【发布时间】:2014-09-04 01:26:46
【问题描述】:

我正在创建一个 COM 可见的 dll,并试图重载一个方法。

所以基本上这段代码:

[ComVisible(true)]
[ProgId("TAF.TextLog")]
[Guid("af3f89ed-4732-4367-a222-2a95b8b75659")]
public class TextLog
{
    String _logFilePath;

    public TextLog()
    {
    }

    [ComVisible(true)]
    public void Create(string filePath)
    {
        String path = Path.GetDirectoryName(filePath);
        if (Directory.Exists(path))
        {
            _logFilePath = filePath;
        }

    [ComVisible(true)]
    public void Write(string message)
    {
        WriteMessage(null, message, AlertMsg.MsgTypes.Info);
    }

    [ComVisible(true)]
    public void Write(string title, string message, AlertMsg.MsgTypes messageType)
    {
        WriteMessage(title, message, messageType);
    }

    private void WriteMessage(string title, string message, AlertMsg.MsgTypes messageType)
    {
        using (StreamWriter file = new StreamWriter(_logFilePath, true))
        {
            if (title == null)
                file.WriteLine(String.Format("{0:yyyy-MM-dd HH:mm:ss}\t{1}", DateTime.Now, message));
            else
                file.WriteLine(String.Format("{0:yyyy-MM-dd HH:mm:ss}\t{1}\t{2}\t{3}", DateTime.Now, title, message, messageType));
        }
    }
}

看起来这是不可能的。如果我从调用程序调用 .Write(顺便说一句,这是一个非常简单的 VBSCript),我会收到一个错误,表明我的参数不正确。

这是调用VBscript的代码:

Set myObj = CreateObject("TAF.TextLog")
myObj.Create("C:\temp\textlog.txt")
myObj.Write "title", "test message 1", 1

如果我在 dll 中只有一个 .Write 方法,它可以正常工作。有人可以告诉我这样的重载是否可以在 dll 中实现?

【问题讨论】:

    标签: c# dll vbscript overloading


    【解决方案1】:

    COM 不支持成员重载,每个名称​​必须是唯一的。 IDispatch::GetIDsOfNames() 不可避免的副作用。脚本解释器用来将脚本代码中使用的“Write”转换为 dispid 的函数。该方法仍然存在,只是没有办法让 GetIDsOfNames() 返回它的 dispid。类型库导出器通过重命名重载方法解决了这个问题,它将是Write_2()

    当您使用后期绑定 afaik 时,没有解决方法,您必须避免重载。

    【讨论】:

    • 好的。感谢您解释这一点。
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      相关资源
      最近更新 更多