Kat Lim Ruiz
Kat Lim Ruiz

Reputation: 2562

NET1.1 Conditional Compilation

I hope this is a very easy one to solve.

I have a crypto library that will be used for NET1.1 apps and higher. The X509Certificate class is in NET1.1 and X509Certificate2 is in NET2.0 and above.

I want to do something like:

#if NET1.1
public void LoadKeys(X509Certificate cert)
{
....
}
#else
public void LoadKeys(X509Certificate2 cert)
{
....
}
#endif

What I'm missing is the compiler symbol!

I found that NET20, NET30 and NET40 exist. However I want to do "if NET1.1, do this; otherwise, go with the advanced model".

Also, bonus question :), do I have to compile in NET1.1 and NET2.0 separately for this to work as intended?

Or can I compile the DLL in NET1.1 and then put in a machine with only NET2.0, and hopefully the ILM will go to the new framework?

Thanks

Upvotes: 0

Views: 490

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499790

Your code already does what you want - if there's no "NET1.1" symbol defined, it will use the second piece of code.

And yes, you have to compile separately for 1.1 and 2.0 for this to work, because you'll end up with different results - one will end up with only the first overload; one will end up with only the second overload.

You probably could run the 1.1 code in 2.0 and higher - that's the normal case - but as you can't have a single build which contains both versions of the method and is 1.1-compatible, you might as well use two builds.

One option is for both methods to appear in the .NET 2+ build though, to retain backward compatibility for apps originally built against the 1.1 build which want to migrate:

public void LoadKeys(X509Certificate cert)
{
....
}
#if !NET1.1
public void LoadKeys(X509Certificate2 cert)
{
....
}
#endif

EDIT: Just to clarify a few points from the discussion with Artur in comments:

  • Artur's blanket statement that you can't use a .NET 1.1 in a .NET 4 application is definitely wrong. I've just done so with the .NET 1.1 build of NUnit (which was the simplest .NET 1.1 library I had to hand)
  • If you're relying on Code-Access Security then there have been changes there between framework versions; you'd have to look at the details of what you're doing to see whether the differences are significant
  • You can't build a .NET 2 assembly and run it in .NET 1.1 due to binary format changes - it's only the other way round that works.
  • While it's possible that you're relying on a call in .NET 1.1 which has been removed from a later version of .NET, that's a pretty rare case, and should be reasonably easy to determine. In general Microsoft tries to maintain backward compatibility pretty well.

You might also want to read the .NET 4 Migration Guide.

Finally, I'd point out that .NET 1.1 is really old now - if you know you need to support it, then that's fair enough, but personally I'd try to get clients to upgrade if at all possible :)

Upvotes: 1

Related Questions