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

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

问题描述

例如

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?

推荐答案

简而言之,它很有用。什么是COM分发标识符?何时在.NET上下文中需要它们? ,但仅用于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天全站免登陆