Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 695
Matt Whitfield Avatar asked Jan 29 '10 09:01

Matt Whitfield


People also ask

Is Auto determined at compile time?

Even when a variable is declared auto, its type is fixed at compile time.

When function is selected for execution at the compilation time is called as?

In computing, compile-time function execution (or compile time function evaluation, or general constant expressions) is the ability of a compiler, that would normally compile a function to machine code and execute it at run time, to execute the function at compile time.

Are templates resolved at compile time?

All the template parameters are fixed+known at compile-time. If there are compiler errors due to template instantiation, they must be caught at compile-time!


2 Answers

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.

like image 126
Jon Skeet Avatar answered Oct 06 '22 01:10

Jon Skeet


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.

like image 30
Mark Seemann Avatar answered Oct 06 '22 00:10

Mark Seemann