自定义编译器警告 [英] Custom Compiler Warnings

查看:195
本文介绍了自定义编译器警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在.NET中使用它可以让你的编译器警告,告诉您该对象/方法/属性已过时,其他的财产以后应该使用ObsoleteAtribute。我目前工作的,需要大量的重构的前雇员code的一个项目。我想写,我可以用它来标记方法或属性,将产生编译器警告,让我写消息的自定义属性。事情是这样的。

When using the ObsoleteAtribute in .Net it gives you compiler warnings telling you that the object/method/property is obsolete and somthing else should be used. I'm currently working on a project that requires a lot of refactoring an ex-employees code. I want to write a custom attribute that I can use to mark methods or properties that will generate compiler warnings that give messages that I write. Something like this

[MyAttribute("This code sux and should be looked at")]
public sub DoEverything(){}

我想这生成编译器警告说,这code sux的,应该看。我知道如何创建一个自定义属性,问题是我怎么会使其产生视觉工作室编译器警告。

I want this to generate a compiler warning that says, "This code sux and should be looked at". I know how to create a custom attribute, the question is how do I cause it to generate compiler warnings in visual studio.

推荐答案

我不相信这是可能的。 ObsoleteAttribute经过特殊处理由编译器和在C#标准被定义。到底是什么原因ObsoleteAttribute不能接受的?在我看来,这样是precisely它是专为这种情况,达到precisely您所需要的!

I don't believe it's possible. ObsoleteAttribute is treated specially by the compiler and is defined in the C# standard. Why on earth is ObsoleteAttribute not acceptable? It seems to me like this is precisely the situation it was designed for, and achieves precisely what you require!

另外请注意,Visual Studio中拾取由ObsoleteAttribute产生上飞得的警告,这是非常有用的。

Also note that Visual Studio picks up the warnings generated by ObsoleteAttribute on the fly too, which is very useful.

不要意思是无益的,只是想知道,为什么你不热衷于使用它......

Don't mean to be unhelpful, just wondering why you're not keen on using it...

不幸的是ObsoleteAttribute是密封的(可能是部分原因是由于特殊处理),因此你不能从它继承自己的属性。

Unfortunately ObsoleteAttribute is sealed (probably partly due to the special treatment) hence you can't subclass your own attribute from it.

从C#标准: -

属性过时用于标记   类型的类型和成员应   不再被使用。

The attribute Obsolete is used to mark types and members of types that should no longer be used.

如果一个程序使用的类型或成员   这是装饰与作废   属性时,编译器会发出   警告或错误。具体而言,   编译器的问题,如果没有错误警告   参数被提供,或者如果误差   参数被设置,并具有   值false。编译器会发出   如果错误参数错误   指定和值为true。

If a program uses a type or member that is decorated with the Obsolete attribute, the compiler issues a warning or an error. Specifically, the compiler issues a warning if no error parameter is provided, or if the error parameter is provided and has the value false. The compiler issues an error if the error parameter is specified and has the value true.

那岂不是总结您的需要吗?......你不会做的比,我不认为更好的。

Doesn't that sum up your needs?... you're not going to do better than that I don't think.

这篇关于自定义编译器警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆