加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#自定义属性命名

发布时间:2020-12-15 06:18:27 所属栏目:百科 来源: By convention, the name of th
导读:我有一个自定义属性类,我定义为: [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]public sealed class MyCustomAttribute : System.Attribute{ ...} 从微软网站: By convention,the name of the attribute class ends with the wor
我有一个自定义属性类,我定义为:
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public sealed class MyCustomAttribute : System.Attribute
{
    ...
}

从微软网站:

By convention,the name of the
attribute class ends with the word
Attribute. While not required,this
convention is recommended for
readability. When the attribute is
applied,the inclusion of the word
Attribute is optional.

所以,属性可以由任一个使用

[MyCustom()]

要么

[MyCustomAttribute()]

我的问题给大家,如果有人使用名称的缩写版本和全名遇到任何问题吗?我正在运行4.0框架.

谢谢!

解决方法

没问题.

在编译的IL中,名称总是附加属性(完整的类型名称).

基于ECMA-334(C#spec)24.2,首先通过精确匹配来完成分辨率,然后通过在[]中给出的名称附加“Attribute”.另外,如果存在冲突(例如:MyAttribute和MyAttributeAttribute),则会在使用“MyAttribute”时生成编译时错误.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读