default property when Com interface type is IDispatch

709 views Asked by At

In a c# com interface, one can define a default member like this

[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComVisible(true)]
public interface IMyComClass
{
    [DispId(0)] string Item{get;}
}

with IDispatch (or dual) everything works as expected, and from VBA I can omit the property like this

Dim o1 As New MyComClass
Debug.Print o1 'this is equivalent to o1.Item

but if I define the interface only as IUnknown, it doesn't work. In the excel Object Browser I still see the property marked with the 'blue dot', and labelled as 'Default Member'. But .Item must be explicitly specified in VBA code.

Is there a way to have default properties in IUnknown behave like in IDispatch?

1

There are 1 answers

0
Hans Passant On BEST ANSWER

No, default properties are very much a notion that can only apply to IDispatch derived interfaces. The late binding you use in the Debug.Print statement cannot work on IUnknown interfaces, the coclass that implements the interface doesn't have the machinery required to call a function selected by a number.

Review the IDispatch::Invoke() method, that's the one that gets the job done. Using the default member is simply done by passing 0 for the first argument.

Dismiss quirks you'd see in the Object Browser, it completely assumes that COM objects support Automation.