我偶然发现了以下C#代码:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public class Marker : Attribute
{
}我们使用SonarLint和一个它的规则,它说类应该以单词Attribute作为前缀。
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public class MarkerAttribute : Attribute
{
}我在想,是不是把Marker的名字改成了MarkerAttribute?因为在使用该属性时,可以跳过Attribute部件。另一方面,当在代码中使用该属性时,您需要整个名称,而不是它将被破坏。
如果这被认为是一种彻底的改变,那么什么才是处理这一问题的最佳方法?因为这是:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public class MarkerAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public class Marker : MarkerAttribute { }使用时会出现下列编译错误:
CS1614 'Marker‘在'Marker’和'MarkerAttribute‘之间存在歧义;使用'@Marker’或‘MarkerAttribute’
发布于 2017-04-21 13:34:32
属性是元数据。在他们自己的属性上什么也做不了。当某些代码读取属性时,属性就会变得有用。为了读取属性值,代码应该使用属性类名。例如,通过GetCustomAttribute呼叫:
Maker maker = yourType.GetCustomAttribute<Maker>();因此,重命名属性类是一项重大更改。为了传递遵从性检查规则,您应该重命名属性类。这里没有选择。
请注意,如果您要在这里继承:
public class Marker : MarkerAttribute { }然后,您将得到完全相同的SonarLint规则破坏-您将得到两个属性类,其中一个将是不兼容的。
https://stackoverflow.com/questions/43544001
复制相似问题