C#自定义属性命名自定义、属性

2023-09-03 06:25:58 作者:学霸不是梦

我有一个自定义属性,我定义为类的:

I have a custom Attribute class that I defined as:

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public sealed class MyCustomAttribute : System.Attribute
{
    ...
}

从微软网站:

From the microsoft website:

按照惯例,该名   属性类的单词结尾   属性。尽管不是必需的,这   惯例被推荐用于   可读性。当属性是   应用中,该词   属性是可选的。

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.

由任于是,该属性可以使用

So, the attribute can be use by either

[MyCustom()]

[MyCustomAttribute()]

我想问你的一切,就是如果有人经历了使用这个名字的缩写版本VS全名的任何问题?我正在运行4.0框架。

My question to you all, is if anyone has experienced any problems with using the abbreviated version of the name vs the full name? I am running 4.0 framework.

谢谢!

推荐答案

没有问题。

在下方编译IL总是有属性附加到它(完整的类型名称)的名称。

Underneath in compiled IL the name always has Attribute appended to it (the full type name).

href="http://www.ecma-international.org/publications/files/ECMA-ST-WITHDRAWN/ECMA-334,%203rd%20edition,%20June%202005.pdf">ECMA-334 (C#规格)24.2,分辨率首先被完全匹配完成的,然后通过添加属性,以在[]中给出的名称。此外,如果有冲突。(例如:MyAttribute,和MyAttributeAttribute)当MyAttribute将被用来产生编译时间错误

Based on ECMA-334 (C# spec) 24.2, the resolution is done first by exact match, then by appending "Attribute" to the name given in []. Additionally, if there is a conflict (ex: MyAttribute, and MyAttributeAttribute) a compile time error is generated when "MyAttribute" would be used.