什么是用C#DISPID注释?注释、DISPID

2023-09-03 02:02:30 作者:只要学不死,就往死里学!

例如

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?

推荐答案

总之,是的,它是有用的,但只适用于COM调度:

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

The DispIdAttribute (从MSDN):

指定COM调度标识符的方法,字段或属性(DISPID)。

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

这个属性包含D​​ISPID的方法,字段或属性它描述。独特的DISPID一般会分配由公共语言运行库,但你可以使用这个属性来指定DISPID分配的方法。当导入类型库,这个属性被应用到与分配的DISPID的所有方法。这确保相同的方法中的任何管理执行保留相同的DISPID如果暴露在COM

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.