views:

111

answers:

3

I have some classes, which have several methods which I don't really want to be there, but are there simply because the XML Serializer needs them. Is there anyway to generate compile-time errors/warnings if they get called from user-code?

I am aware that I can implement IXmlSerializable, and I am also aware that I can separate out the classes into purely data storage classes, however, I am not asking a question about how I should design such a system, I am simply asking if there is a way to generate compile-time errors/warnings if they are called by anything that is not the XML serializer...

+14  A: 

You can add

[Obsolete]

to the method. The IsError property of ObsoleteAttribute controls whether an error or warning is generated, and you can provide an explanatory message too.

Jon Skeet
Excellent, thanks
Matt Whitfield
[Obsolete], serves a different purpose than what OP intends. I don;t think this is the way this attribute is meant to be used.
Pop Catalin
@Pop: sure, the purpose is perhaps slightly different, but the result is probably exactly what was asked for.
Fredrik Mörk
Sorry, one further question - is there any way that I can apply something similar to the set mutator of a property, such that you can get the value but not set it, or would the best solution to be to define a separate property, and mark the whole property as obsolete?
Matt Whitfield
@Matt: I'm not sure, I'm afraid. I suggest you have a play with it :)
Jon Skeet
@Matt the targets for ObsoleteAttribute are: AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Delegate Wich don't include event an property accesors. The answer is no, you can't add the ObsoleteAttribute to property accessors.
Pop Catalin
+4  A: 

You could decorate the members in question with the ObsoleteAttribute. Its intention is a bit different, but it will generate compiler warnings (or errors) when called from user code.

Mark Seemann
A: 

You can hide the methods from users intellisense using the [EditorBrowsable] attribute, and from property designer using [Browsable], attribute.

I don't recommend using the [ObsoleteAttribute], because it conveys a different meaning to what method state actually is. Instead use a comment indicating that the method should not be used from user code.

Also keep in mind that there are lot's of users that compile their code with threat warnings as errors, which will make impossible for them to compile valid code, in this case.


Pop Catalin
ObsoleteAttribute conveys a slightly different meaning, but has the desired behaviour. I'd argue that EditorBrowsable doesn't actually have the meaning of "you shouldn't call this from user code" either - and it doesn't generate warnings or errors, which is the desired behaviour in this case.
Jon Skeet
Jon's answer is pretty much spot on for me, because I am refactoring some old code, and I don't have the time to completely re-design it right now, so if I can shake out poor object usage patterns with compile time errors, that's a big win for me - certainly not the ideal computer zen amazingness, but a big win none the less.
Matt Whitfield
@Matt Whitfield +1 for pragmatism
Sam Holder