2

I have a class from project A (.NET 4) which uses the the AggregateException class. I am now importing this same class into project B which i s.NET 3.5. AggregateException class does not exist for 3.5. Is there a way to use #if and reflection or something to ignore the catch(AggregateException) if the project in which it exists is not .NET 4?

Brad
  • 11,934
  • 4
  • 45
  • 73

2 Answers2

1

No - you'd need to make a separate version of the project which targets .NET 3.5 if you want to use it from a .NET 3.5 application.

You can't load a .NET 4 assembly, even if you don't use .NET 4 features, from .NET 3.5.

The only place an #if directive may help would be if you wanted to setup your file to have a define based on the platform target. This would have to happen inside of the file in your Project A project, and you'd need two projects (which could include the same source files), as a single project can't be setup to target two separate framework versions. (You can always change the framework, but this is not always trivial...)

That being said, this is likely to not work directly, anyways. If your .NET 4 library is catching an AggregateException, that means it's using some functionality that raises that exception - that method call is likely a .NET 4 only feature, as this exception is new in .NET 4, and typically only occurs when using something like the TPL.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
0

The way that we handle this sometimes is through creative use of conditional compiles and stub classes.

In your case what we would probably do is add a stub class like:

namespace System
{
    class AggregateException: Exception
    {
    }
}

and just include this class in the 3.5 project.

competent_tech
  • 44,465
  • 11
  • 90
  • 113