COM-> .NET - 无法访问重载方法 [英] COM->.NET - can't access overloaded method

查看:110
本文介绍了COM-> .NET - 无法访问重载方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从COM(jscript)访问(图像调整器) .Net库。

I'm trying to access a .Net library (The Image Resizer) from COM (jscript).

我已经尝试了IDispatch和类接口生成,以及相关类的[ClassInterface(ClassInterfaceType.AutoDual)]。

I've tried both IDispatch and class interface generation, as well as [ClassInterface( ClassInterfaceType.AutoDual)] on the class in question.

有一个方法有3个重载:

There is a method with 3 overloads:

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运行时错误) )

The following both generate "Wrong number of arguments or invalid property assignment" (JScript runtime error)

Build("file","file", s) 
Build("file","file","settings

我找不到任何因为重载不能通过互操作工作的原因,尤其是当arg计数不同。
我错过了什么?

I can't find any reason that overloads shouldn't work through interop, especially when the arg count differs. Am I missing something?

更新:这是方法定义的完整代码。第二个重载是不可访问的。它不仅仅是这些方法 - 在每个重载方法中,我似乎只能访问第一个重载。这是一个未记录的COM错误/设计缺陷吗?

Update: Here is the full code of the method definitions. The second overload is inacccessible. It's not just these methods - in every overloaded method, I only seem to be able to access the first overload. Is this a undocumented COM bug/design flaw?

    /// <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没有做方法重载。

True that COM doesn't "do" method overloading.

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

这是一个关于FxCop的文档页面,这是一个静态分析工具。但是那里有一些信息,这对COM开发人员很有用:

This is a doc page on FxCop, a static analysis tool. But there's a tidbit of information there, which is useful for COM developers:


当重载方法暴露给COM客户端时,只有第一种方法超载保留其名称。通过在名称旁边附加一个下划线字符'_'和一个与重载声明顺序相对应的整数来唯一地重命名后续重载。

When overloaded methods are exposed to COM clients, only the first method overload retains its name. Subsequent overloads are uniquely renamed by appending to the name an underscore character '_' and an integer that corresponds to the order of declaration of the overload.

还可以看到
COM互操作中的重载(CCW) - IDispatch名称包括后缀(_2,_3等)

因此,通过COM层,您可以使用

So, through the COM layer, you could call your original methods with

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

这篇关于COM-&gt; .NET - 无法访问重载方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆