2011-05-17 19 views
1

我試圖從COM(jscript)訪問.Net庫(The Image Resizer)COM - > .NET - 無法訪問重載的方法

我已經嘗試過IDispatch和類接口生成,以及有問題的類上的[ClassInterface(ClassInterfaceType.AutoDual)]。

有一個與3個重載的方法:

Bitmap Build(object, ResizeSettings settings) 
void Build(object source, object dest, string settings) 
void Build(object source, object dest, ResizeSettings settings) 

調用

Build("file",s); //works 

以下二者生成(JScript運行時錯誤)

Build("file","file", s) 
Build("file","file","settings 
「參數或無效的屬性賦值錯誤數」

我找不到任何重載不應該通過互操作的原因,especi當arg計數不同時盟友。 我錯過了什麼嗎?

更新:這裏是方法定義的完整代碼。第二次過載不可訪問。這不僅僅是這些方法 - 在每個重載方法中,我似乎只能訪問第一個重載。這是一個沒有記錄的COM錯誤/設計缺陷嗎?

/// <summary> 
    /// Provides methods for generating resized images, and for reading and writing them to disk. 
    /// Use ImageBuilder.Current to get the current instance (as configured in the application configuration), or use ImageBuilder.Current.Create() to control which extensions are used. 
    /// </summary> 
    public class ImageBuilder : AbstractImageProcessor, IQuerystringPlugin 
    { 


     /// <summary> 
     /// Resizes and processes the specified source image and returns a bitmap of the result. 
     /// This method assumes that transparency will be supported in the final output format, and therefore does not apply a matte color. Use &amp;bgcolor to specify a background color 
     /// if you use this method with a non-transparent format such as Jpeg. 
     /// </summary> 
     /// <param name="source">May be an instance of string (a physical path), VirtualFile, IVirtualBitmapFile, HttpPostedFile, Bitmap, Image, or Stream.</param> 
     /// <param name="settings">Resizing and processing command to apply to the.</param> 
     public virtual Bitmap Build(object source, ResizeSettings settings) { 
      BitmapHolder bh = new BitmapHolder(); 
      Build(source, bh, settings); 
      return bh.bitmap; 
     } 

     /// <summary> 
     /// Resizes and processes the specified source image and stores the encoded result in the specified destination. 
     /// </summary> 
     /// <param name="source">May be an instance of string (a physical path or app-relative virtual path), VirtualFile, IVirtualBitmapFile, HttpPostedFile, Bitmap, Image, or Stream. app-relative virtual paths will use the VirtualPathProvider system</param> 
     /// <param name="dest">May be a physical path (string), or a Stream instance. Does not have to be seekable.</param> 
     /// <param name="settings">Resizing and processing command to apply to the.</param> 
     public virtual void Build(object source, object dest, ResizeSettings settings) { 
      ResizeSettings s = new ResizeSettings(settings); 
+0

我覺得你的COM組件的代碼(或至少界面)在這裏更重要......你可以發佈嗎? – fretje 2011-05-17 13:53:31

+0

組件是.NET,調用者是COM。我會添加更多的代碼。 – 2011-05-17 14:00:58

+0

我不明白。 jscript不是COM。它可以很容易地調用COM組件。如果你希望.NET組件可以作爲COM組件訪問,你必須爲你的.NET組件添加代碼(比如'ClassInterface(ClassInterfaceType.AutoDual)'你在談論你的問題)。這使.NET組件成爲COM服務器。 – fretje 2011-05-17 14:04:59

回答

4

確實COM沒有「執行」方法重載。

但是。見http://msdn.microsoft.com/en-us/library/ms182197(v=vs.80).aspx

這是一個靜態分析工具FxCop的文檔頁面。但是這裏有一些信息,對COM開發人員很有用:

當重載方法暴露給COM客戶端時,只有第一個方法重載保留其名稱。後續重載通過在名稱後面附加下劃線字符'_'和一個與重載聲明順序相對應的整數來唯一地重命名。

,也看到
Overloads in COM interop (CCW) - IDispatch names include suffix (_2, _3, etc)

那麼,通過COM層,你可以打電話給你的原始方法與

Build_2("file", "file", s); 
Build_3("file", "file", settings); 
+0

哇...太棒了!謝謝! – 2011-08-22 09:17:31

3

重載不適用於互操作層到COM。但是,您可以使用可選參數並隱藏COM層中的所有其他方法:

// COM won't see this. 
[ComVisible(false)] 
void Test(string a) 

// COM will see this and parameter b is not required 
void Test(string a, [DefaultParameterValue(null)] string b) 
+0

乾淨的主意!它必須與.NET 2兼容,但它是一個庫。也許我將不得不公開一個備用接口。 – 2011-05-18 18:03:12

+0

我在.NET 2.0應用程序中使用了同樣的想法。即使艱難的可選參數在C#2.0中也不受支持,該屬性在那裏,可以應用於參數。只是C#編譯器沒有對它做任何魔術。 – 2011-05-18 18:09:01

+0

真的嗎?甜。我剛剛學到了非常酷的東西! – 2011-05-18 19:26:50