7

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...

Matt Whitfield
  • 6,436
  • 3
  • 29
  • 44
  • Also see http://stackoverflow.com/questions/968249/c-create-custom-warning-in-visual-studio-if-certain-method-is-used-in-source-co – Dirk Vollmar Jan 29 '10 at 09:28

3 Answers3

14

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • [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 Jan 29 '10 at 09:28
  • @Pop: sure, the purpose is perhaps slightly different, but the result is probably exactly what was asked for. – Fredrik Mörk Jan 29 '10 at 09:30
  • 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 Jan 29 '10 at 09:31
  • @Matt: I'm not sure, I'm afraid. I suggest you have a play with it :) – Jon Skeet Jan 29 '10 at 09:42
  • 2
    @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 Jan 29 '10 at 09:44
5

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
  • 225,310
  • 48
  • 427
  • 736
0

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
  • 61,751
  • 23
  • 87
  • 115
  • 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 Jan 29 '10 at 09:41
  • 1
    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 Jan 29 '10 at 09:45
  • @Matt Whitfield +1 for pragmatism – Sam Holder Jan 29 '10 at 10:52