COM-> .NET - 无法访问重载方法

时间:2011-05-17 13:41:52

标签: javascript interop com-interop typelib

我正在尝试从COM(jscript)访问.Net库(The Image Resizer)

我已经尝试了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

我找不到任何因为重载不应该通过互操作的原因,特别是当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);

2 个答案:

答案 0 :(得分:4)

确实COM没有“做”方法重载。

BUT。见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);

答案 1 :(得分: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)