Determining .Net method suffix number in VBScript (COM-interop)

孤街浪徒 提交于 2019-11-27 08:01:34

问题


You can use .NET methods through COM-interop in VBScript. You have to append a certain suffix number to the method since overloads don't cross the managed/unmanaged boundary. The suffix number doesn't seem to have a particular order...how is the suffix number determined?

Example:

Dim encoding, bytesthroughdotnet
Set encoding = CreateObject("System.Text.UTF8Encoding")
    bytesthroughdotnet = encoding.GetBytes_4("你好Ğ") 'get bytes
    WScript.Echo LenB(bytesthroughdotnet) 'length
Set encoding = Nothing

How come _4 is used for GetBytes?

(This question follows this response )


回答1:


Microsoft document Exported Member Conversion -- Overloaded Methods already explain all. In brief, @Nilpo 's answer is right, quickest way is usually just trial and error.

Overloaded Methods

Although .NET supports overloaded methods, the IDispatch interface relies solely on method name for binding, rather than the complete method signature. It is therefore not capable of supporting overloaded methods. However, to provide access to overloaded methods of a type, Tlbexp.exe decorates the names of overloaded methods with an ordinal number so that each method name is unique.

The following managed and unmanaged signatures show the inclusion of numbers:

Managed signature

interface INew { public:
    void DoSomething();
    void DoSomething(short s);
    void DoSomething(short l);
    void DoSomething(float f);
    void DoSomething(double d);
}

Unmanaged signature

 interface INew {
    void DoSomething();
    void DoSomething_2(short s);
    void DoSomething_3(short l);
    void DoSomething_4(float f);
    void DoSomething_5(double d);
}

The COM signature for the methods appears as a single DoSomething method followed by a series of decorated DoSomething_x methods, where x starts at 2 and increments for each overloaded form of the method. Note that some of the overloaded methods can be inherited from a base type. However, there is no guarantee that overloaded methods will retain the same number as the type version advances.

Although .NET clients can use the overloaded form of the method, COM clients have to access the decorated methods. Object browsers display all forms of the decorated method with the method signature to enable you to select the correct method. The late-bound client can also call IDispatch::GetIdsOfNames, passing in the decorated name to get the DispID of any overloaded method.




回答2:


Since VBScript does not have support for overloaded methods, each overloaded method in a class is named uniquely using numbers appended to their name. They are numbered in the order in which they are defined in the original class. More information in my article Using .Net Interops in VBScript on ASP Free.



来源:https://stackoverflow.com/questions/9755263/determining-net-method-suffix-number-in-vbscript-com-interop

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