2014-09-03 37 views
2

我正在創建一個COM可見的dll,我試圖重載一個方法。在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)); 
     } 
    } 
} 

看起來這是不可能的。然而。如果我從調用程序(這是一個非常簡單的VBSCript)調用.Write,我得到一個錯誤,我的參數不正確。

這是調用VBScript代碼:

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

如果我只有一個DLL中的.WRITE方法,它工作正常。有人能告訴我,如果像這樣超載甚至可能在一個DLL?

回答

2

COM沒有爲成員重載的支持,每名必須是唯一的。 IDispatch::GetIDsOfNames()的不可避免的副作用。腳本解釋器用於將腳本代碼中使用的「寫入」翻譯爲虛假的函數。該方法仍然存在,沒有辦法讓GetIDsOfNames()返回它的dispid。類型庫導出程序通過解決此問題重載的方法,它將是Write_2()

沒有解決方法,當您使用遲綁定afaik時,您必須避免重載。

+0

好的。謝謝你解釋這個。 – Mytzenka 2014-09-04 03:51:24