Reputation: 2562
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
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:
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