Adding ObsoleteAttribute to (or otherwise greylisting) Types not under my control?
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)
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?
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...
Edit: Just to clarify, I do not want to block Types from using (keep in mind that Obso开发者_JS百科leteAttribute 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.
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 { }
}
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.
No, and i tmakes sense. As they are not under your control, you are not having a right to obsolete them ;) So to say.
FxCop can not do it either. xCop can say whether you use one, but that is not the same as adding an obsolete attribute to them.
I seriously think the whole idea of types "I do not want to use in my .net project" is pretty out - becasue you will have to do a TON of work for NO result. Types not usefull are not used by definition (unless you like including stuff that you dont really need). Obsoleting them does not achieve anything.
And you may be surprised... maybe one day you need some of them.
Never been in a project where this approach was taken.
精彩评论