开发者

Is there any way to stop a method being called at compile-time?

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 seriali开发者_开发知识库zer...


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.


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.


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.


0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜