17

for example

public interface IWMPSettings

        [DispId(101)]
        bool autoStart { get; set; }
        [DispId(102)]
        int balance { get; set; }

is it useful or is it just auto-generated for compiler? What are COM dispatch identifiers for and when would they be needed in a .NET context?

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
IAdapter
  • 62,595
  • 73
  • 179
  • 242

1 Answers1

15

In short, yes it is useful, but only for COM dispatch:

The DispIdAttribute (from MSDN):

Specifies the COM dispatch identifier (DISPID) of a method, field, or property.

This attribute contains the DISPID for the method, field, or property it describes. Unique DISPIDs are typically assigned by the common language runtime, but you can use this attribute to assign a specific DISPID to a method. When importing a type library, this attribute is applied to all methods with assigned DISPIDs. This ensures that any managed implementation of the same method retains the same DISPID if exposed to COM.

Community
  • 1
  • 1
Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
  • I don't understand. I'm not c# programmer. What that means for a me as developer? – IAdapter Jan 17 '12 at 10:07
  • 4
    @IAdapter If your types are COM visible and you expect them to be invoked via COM code (not .Net) then you *may* need to use this attribute. It will not affect any code that calls these methods from .Net however, so if your code is all .Net, the attribute is redundant. – Rich O'Kelly Jan 17 '12 at 10:13
  • 2
    Is this the right way to follow some of the above: Basically if you are going to take each of your methods, properties, and fields and implement it only one time, in the same assembly, in the same class, etc., this is unnecessary, *even* when using COM in VB6 or whatever else to access it. However if you were to take a method, field, or property and implement it in different dlls or do something else like that (maybe supporting different platforms manually, maybe porting old code to a newer language, etc.), *then* that is when this comes in really handy, as it will maintain the same identity. – Panzercrisis Oct 23 '17 at 18:59
  • Is that the correct context and everything of the quote above? – Panzercrisis Oct 23 '17 at 19:01
  • 3
    @Panzercrisis I agree with your comments, I don't think this really answers the intent of the question either (thought it does so in a strict sense... it doesn't convey when these DispIDs are useful) – StayOnTarget Jan 08 '19 at 18:12