Search code examples
c#com-interopidl

Is there a default default property?


I'm exporting a C# class to VB6 through COM interop. It seems to have a default COM property without me ever having set one!

The C# class looks like:

[ComVisible(true), ClassInterface(ClassInterfaceType.AutoDual)]
class Foo
{
     public int I { get; set; }
     public bool B { get; set; }
     public int Value { get; set; }  // <--- the culprit
     public string S { get; set; }
}

(I know why should I not use autodual - please overlook that, its useful for this stage of development)

The property Value is behaving as the default in VB6. It is shown this way in object browser. But notice that I have not set a DispId attribute to indicate the default.

Also, in OLEView you can see that in the IDL of the TLB for the assembly the Value property has attribute [id(00000000), propget]. This is equivalent to DISPID_VALUE. So, for some reason the compiler or TLBEXP or something is explicitly making Value into the default.

Why is this happening? I actually don't want a default property and its existence was masking a coding mistake which "just worked", basically by accident.


Solution

  • As stated by Hans Passant in comments:

    Tlbexp.exe does that. View the type library with oleview.exe, File > View TypeLib.

    [dispid] auto-generation is not documented, but this is not horribly insensible. Use the [DispId] attribute to override.

    [Tlbexp does seem to look for a property named "Value" to use for this]. Could be interesting if you give the class an indexer btw.