COM->.NET - can't access overloaded method

百般思念 提交于 2019-12-31 03:50:22

问题


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

回答1:


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



回答2:


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)  


来源:https://stackoverflow.com/questions/6031668/com-net-cant-access-overloaded-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!