为什么需要在 c# 中使用 DispId 注释? [英] Why would you need to use the DispId annotation in c#?

查看:56
本文介绍了为什么需要在 c# 中使用 DispId 注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如

public interface IWMPSettings

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

它有用还是只是为编译器自动生成的?什么是 COM 调度标识符?在 .NET 上下文中何时需要它们?

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?

推荐答案

简而言之,是的,它很有用,但仅适用于 COM 调度:

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

DispIdAttribute(来自 MSDN):

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

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

此属性包含它所描述的方法、字段或属性的 DISPID.唯一 DISPID 通常由公共语言运行时分配,但您可以使用此属性将特定 DISPID 分配给方法.导入类型库时,此属性适用于所有分配了 DISPID 的方法.这确保了相同方法的任何托管实现在暴露给 COM 时都保留相同的 DISPID.

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.

这篇关于为什么需要在 c# 中使用 DispId 注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆