Is there a way to create a custom preprocessor directive in C# to influence on the process of compilation? If there's no built-in support for defining a new directive, could you suggest a workaround to simulate similar behaviour?
Asked
Active
Viewed 4,407 times
0
-
Are you saying you want a new directive like `#doSpecalStuff` or do you mean something like `#if MY_DIRECTIVE` and you want to define `MY_DIRECTIVE`? – Scott Chamberlain Dec 03 '16 at 20:01
-
3No, there's nothing like that. It's very hard to suggest a workaround with no clue what kind of behavioral change you're trying to make. – Jon Skeet Dec 03 '16 at 20:02
-
@scott-chamberlain, yes, you understand it completely right. I actually want to create a new directive. – NamiraJV Dec 03 '16 at 20:08
-
@NamiraJV Okay - what do you want this new directive to do? – jdphenix Dec 03 '16 at 20:09
-
Then how do you expect us to give you a work around if you have not explained the behavior you want to do? – Scott Chamberlain Dec 03 '16 at 20:09
-
@jon-skeet, I want to make compiler generate some code depending on the directive and its parameters – NamiraJV Dec 03 '16 at 20:10
-
3So you want to have some token that means "given these parameters, do this thing". Why is a function call not appropriate here? – jdphenix Dec 03 '16 at 20:11
-
It sounds like either you should be writing regular code, or modifying the compiler itself. There's nothing like general purpose macros in C#. – Jon Skeet Dec 03 '16 at 20:14
-
For example, OpenMP for C/C++ uses the form of directives to provide its functionality. I was just wondering if we can implement an API in that kind of fashion. – NamiraJV Dec 03 '16 at 20:18
-
Well, nobody's preventing you from writing your own compiler. That's how custom compilation is done. Or just use a code generator, if that makes sense. – Luaan Dec 03 '16 at 20:20
1 Answers
1
No you cannot do that in c#. Here is a list of all directives.
Alternative you maybe can work with the following directives:
You can add defines to the processor like this:
#define xx
And check for them like this
#if (xx)
Console.WriteLine("xx defined");
#else
Console.WriteLine("xx not defined");
#endif
If you are working with roslyn
take a look at this interesting article but be carefull with what you do.
Or take a look at this SO Article which shows you how to implement your custom syntax with the roslyn scripting api.
-
1That's not what the OP is after - they're after *custom* directives, as I understand it. – Jon Skeet Dec 03 '16 at 20:01