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

发布于 2024-11-08 07:17:41 字数 2946 浏览 0 评论 0原文

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

我找不到任何重载不能通过互操作工作的原因,特别是当参数计数不同时。 我错过了什么吗?

更新:这是方法定义的完整代码。第二个过载是无法访问的。不仅仅是这些方法 - 在每个重载方法中,我似乎只能访问第一个重载。这是一个未记录的 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);

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

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

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)

Calling

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

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

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

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

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 &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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相对绾红妆 2024-11-15 07:17:41

确实,COM 不“执行”方法重载。

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

这是静态分析工具 FxCop 的文档页面。但其中有一些信息对 COM 开发人员很有用:

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

另请参阅
COM 互操作 (CCW) 中的重载 - IDispatch 名称包含后缀(_2、_3 等)

因此,通过 COM 层,您可以使用以下命令调用原始方法

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

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

BUT. see http://msdn.microsoft.com/en-us/library/ms182197(v=vs.80).aspx .

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:

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.

and also see
Overloads in COM interop (CCW) - IDispatch names include suffix (_2, _3, etc)

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

Build_2("file", "file", s);
Build_3("file", "file", settings);
我三岁 2024-11-15 07:17:41

重载对于 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)  

Overloading does not work for the interop layer to COM. You could however use optional parameters and hide all other methods from the COM layer:

// 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)  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文