Overloaded Methods

Team-Fly    

 
.NET and COM Interoperability Handbook, The
By Alan Gordon
Table of Contents
Chapter Eight.  Advanced COM to .NET Interop

Overloaded Methods

A managed class can have overloaded methods. A single method name can have multiple argument lists and return values. Unfortunately, COM does not support overloaded methods. The CCW that .NET exposes to unmanaged clients handles this mismatch by exposing one unmanaged method that has the exact name as the overloaded method. It then exposes the other overloaded versions of the method with _N appended to the managed name where N starts at 2. For instance, given the following managed class, which has three different versions of a method called TestMethod:

 using System; using System.Runtime.InteropServices; namespace TestOverload {     public class TestClass     {       public TestClass()       {       }  public string TestMethod(int arg1)   {   return "TestMethod(int arg1)";   }   public string TestMethod(int arg1,string arg2)   {   return "TestMethod(int arg1,string arg2)";   }   public string TestMethod(string arg1,string arg2)   {   return "TestMethod(string arg1,string arg2)";   }  } } 

The CCW will expose the following methods from its automatically generated class interface (_TestClass):

 [id(0x60020004)] HRESULT TestMethod(         [in] long arg1,[out, retval] BSTR* pRetVal); [id(0x60020005)] HRESULT TestMethod_2(         [in] long arg1,[in] BSTR arg2,         [out, retval] BSTR* pRetVal); [id(0x60020006)] HRESULT TestMethod_3(         [in] BSTR arg1,[in] BSTR arg2,         [out, retval] BSTR* pRetVal); 

Unless you use the ClassInterface attribute on the managed class and specify AutoDual for the ClassInterfaceType, these methods will not be exposed directly from the exposed class interface. The class interface instead looks as follows :

 interface _TestClass : IDispatch { }; 

You will only be able to invoke the overloaded methods through the Invoke method of IDispatch. This presents a problem. If you have three overloads of a particular method, you know that the equivalent unmanaged method names will be MethodName, MethodName_2, and MethodName_3. However, if you only have a late-bound interface, you won't know which argument list corresponds to which unmanaged method name. For instance, you may know that you want to call the version of TestMethod method that takes two string arguments, but, unless you use the AutoDual attribute to generate the overloaded methods explicitly, how will you know that the version of the TestMethod method with two string arguments corresponds to TestMethod_3?

You can figure this out in two ways. The first way is to use the ClassInterface attribute (with AutoDual) to generate a late-bound interface temporarily, which will show the mapping of unmanaged method names to the various permutations of the overloaded managed method. In other words, if you add the following line to the TestClass

 [ClassInterface(ClassInterfaceType.AutoDual)] public class TestClass { // Implementation omitted... } 

The Assembly Registration Tool will generate the following class interface:

 interface _TestClass : IDispatch { //... other methods omitted     [id(0x60020004)] HRESULT TestMethod(         [in] long arg1,[out, retval] BSTR* pRetVal);     [id(0x60020005)] HRESULT TestMethod_2(         [in] long arg1,[in] BSTR arg2,         [out, retval] BSTR* pRetVal);     [id(0x60020006)] HRESULT TestMethod_3(         [in] BSTR arg1,[in] BSTR arg2,         [out, retval] BSTR* pRetVal); } 

This interface clearly shows which overloaded argument list corresponds to each unmanaged method name. You don't have to keep the early-bindable version of the assembly around. As soon as you have figured out the mapping of the overloaded managed methods to the various permutations of the overloaded unmanaged methods, you can remove the ClassInterface attribute and go back to accessing your managed components through the late-bound interface. Another way to determine which overload corresponds to a particular unmanaged method is to use the IL Disassembler (ILDASM). Bring up ILDASM on your assembly and make sure that Sort by name and Show Public are the only items selected under the View menu and then view the list of methods for your class. The order in which the methods are displayed tells you which method corresponds to each unique unmanaged name. Using this example, the first method in the list will have the name TestMethod, and the next two will have the names TestMethod_2 and TestMethod_3.


Team-Fly    
Top
 


. Net and COM Interoperability Handbook
The .NET and COM Interoperability Handbook (Integrated .Net)
ISBN: 013046130X
EAN: 2147483647
Year: 2002
Pages: 119
Authors: Alan Gordon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net