【问题标题】:COM->.NET - can't access overloaded methodCOM->.NET - 无法访问重载方法
【发布时间】:2011-08-27 06:18:35
【问题描述】:

我正在尝试从 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);

【问题讨论】:

  • 我认为你的 COM 组件的代码(或者至少是接口)在这里更重要......你能发布吗?
  • 组件是.NET,调用者是COM。我会添加更多代码。
  • 我不明白。 jscript 不是 COM。它可以很容易地调用 COM 组件。如果您希望您的 .NET 组件可以作为 COM 组件访问,您必须为此向您的 .NET 组件添加代码(例如 ClassInterface(ClassInterfaceType.AutoDual) 您在问题中所说的)。这使得 .NET 组件成为 COM 服务器。
  • 发布您在 .Net 组件上定义 com 接口的代码。它可能有问题。我还记得过去我在 COM 接口中重载方法时遇到了麻烦。您可能想尝试将方法重命名为其他名称并调用它。

标签: javascript interop com-interop typelib


【解决方案1】:

确实 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);

【讨论】:

  • 哇……太棒了!谢谢!
【解决方案2】:

重载不适用于与 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)  

【讨论】:

  • 好主意!它必须与 .NET 2 兼容,但它是一个库。也许我必须公开一个备用界面。
  • 我在 .NET 2.0 应用程序中使用了同样的想法。在 C# 2.0 中甚至不支持严格的可选参数,该属性存在并且可以应用于参数。只是 C# 编译器没有对它做任何魔法。
猜你喜欢
  • 2012-05-21
  • 2018-08-05
  • 1970-01-01
  • 1970-01-01
  • 2019-05-09
  • 1970-01-01
  • 2022-07-11
  • 2018-09-21
  • 2010-09-30
相关资源
最近更新 更多