将ObsoleteAttribute添加到不受我控制的类型(或以其他方式列入灰名单)? [英] Adding ObsoleteAttribute to (or otherwise greylisting) Types not under my control?
问题描述
有没有办法将我无法控制的类型标记为过时?基本上,我想将ObsoleteAttribute添加到不想在.net项目中使用的类型(即SerializableAttribute)
Is there a way to somehow mark Types I do not control as Obsolete? Basically I would like to add ObsoleteAttribute to types I do not want to use in my .net Project (i.e., SerializableAttribute)
我相信我可以使用FxCop做类似的事情,但是理想情况下,我希望编译器已经为灰色列表"类型生成了警告?
I believe I can do something like that with FxCop, but ideally I would like to have the compiler already generate warnings for "greylisted" Types?
这应该在每个项目级别上,但是遗憾的是扩展属性"不存在,并且对于ObsoleteAttribute在运行时添加它们为时已晚...
This should be on a Per-Project level, but sadly "Extension Attributes" don't exist, and adding them at Runtime is too late for ObsoleteAttribute...
为了澄清,我不想阻止Types的使用(请记住,ObsoleteAttribute默认情况下只会生成警告,而不是错误!),我只想生成警告当在所有情况下有99%使用它们时,使用它们是错误的(即SerializableAttribute).另外,我不仅指.net类型,还指某些第三方类型.这更是一个提醒.我想FxCop是我最好的选择.
Just to clarify, I do not want to block Types from using (keep in mind that ObsoleteAttribute by default only generates a warning, not an error!), I just want to generate warnings when they are used as in 99% of all cases it's wrong to use them (i.e. SerializableAttribute). Also, I'm not only referring to .net types but also to some third party types. It's more a reminder. I'll guess FxCop is my best bet then.
推荐答案
没有一种直接执行此操作的方法.FxCop将是您最好的选择.不过,您可以发挥创意,然后重新声明类型.警告:这是躲闪的躲闪!但这会起作用:
There isn't a way to do this directly; FxCop would be your best bet. You could get creative, though, and re-declare the type. Caveat: this is dodgy dodgy! But it'll work:
namespace System.Text {
[Obsolete("Pah; who wants performance...", true)]
public class StringBuilder { }
}
这太讨厌了!但是需要 extern别名
才能解决.当然,它也可能带有一些基于字符串的反射( Activator.CreateInstance
).
This is so nasty! But takes an extern alias
to get around. It might also monkey with some string-based reflection (Activator.CreateInstance
) of course.
这篇关于将ObsoleteAttribute添加到不受我控制的类型(或以其他方式列入灰名单)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!