C#自定义属性命名

编程入门 行业动态 更新时间:2024-10-16 00:22:22
本文介绍了C#自定义属性命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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="www.ecma-international/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.

更多推荐

C#自定义属性命名

本文发布于:2023-11-17 00:12:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608095.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   属性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!