Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make a preprocessor directive dependent on the .NET framework version?

Here's a concrete example of what I want to do.

Consider the string.Join function. Pre-.NET 4.0, there were only two overloads, both of which required a string[] parameter.

As of .NET 4.0, there are new overloads taking more flexible parameter types, including IEnumerable<string>.

I have a library which includes a Join function that does essentially what the .NET 4.0 string.Join function does. I was just wondering if I could make this function's implementation dependent on the .NET framework being targeted. If 4.0, it could simply call string.Join internally. If 3.5 or older, it could call its own internal implementation.

  1. Does this idea make sense?
  2. If it does make sense, what's the most logical way to do it? I guess I'm just assuming a preprocessor directive would make the most sense, since a call to string.Join with an IEnumerable<string> parameter won't even compile when targeting a .NET version older than 4.0; so whatever approach I use would have to take place prior to compilation. (Checking the Environment.Version property at runtime, for example, wouldn't work.)
like image 773
Dan Tao Avatar asked Dec 26 '10 22:12

Dan Tao


People also ask

What is meant by preprocessor directive in C# net?

C# preprocessor directives are the commands for the compiler that affects the compilation process. These commands specifies which sections of the code to compile or how to handle specific errors and warnings.

What is #if and #endif in C#?

These are for compiler constants, for example: #if DEBUG Debug.WriteLine("This is written in debug mode"); #endif. If the DEBUG constant is defined, that code gets compiled, if it's not then it's stripped out, ignored by the compiler..it's a way to determine what's in a certain build type, and stripped out for another.

What is the use of #define directive in C#?

#define lets you define a symbol. By using the symbol as the expression passed to the #if directive, the expression evaluates to true . You can also define a symbol with the DefineConstants compiler option. You can undefine a symbol with #undef .


2 Answers

You can take a look at another question on Stack Overflow that illustrates how to set conditional constants through the project file's XML: Detect target framework version at compile time

Then using that you can determine if you should use the .NET 4 overloads or your own library.

like image 128
Joshua Rodgers Avatar answered Sep 27 '22 19:09

Joshua Rodgers


Yes, I think it makes sense (for your particular case, since the change is relatively minor), though obviously that sort of thing could scale out of control fairly quickly.

IMHO, the most logical way to go about it would be to create different solution/project configurations for each version, then define a custom symbol (say, NET40) in your 4.0 configurations, then use that with an #if. I'm not certain if configurations will allow you to change the runtime version (that would obviously be the perfect solution), but your worst-case is having to change the version manually.

EDIT: I just saw the answer linked to in Joshua's answer, and that seems like a more streamlined solution, but I'll leave this here anyway, since it does, strictly speaking, answer the question.

like image 23
Adam Robinson Avatar answered Sep 27 '22 18:09

Adam Robinson