views:

111

answers:

3

Can a .net 3.5 c# project reference a .net 4.0 assembly and compile with it? What about run time? What about 4.0 compiling / running with 3.5?

EDIT: in particular, I have a 3rd party .net assembly. It has extension points: I register my dll in app.config and it calls me (probably via reflection). Can I implement my dll in .net 4.0? when is the CLR version chosen, when the main exe comes up or when a dll needs some version of it?

+5  A: 

No, you need to upgrade to 4.0 to reference a 4.0 assembly.

Otávio Décio
thank - please see my edit
Yaron Naveh
A: 

A .net 3.5 application needs to be retargeted at te 4.0 framework to become a .net 4.0 application and use .net 4.0 features. This shouldn't break anything (apart from requiring .net 4.0 on your user's PCs), but will allow you to start using .net 4.0 features.

You can retarget a .net 4.0 app down to 3.5 iff it doesn't use any .net 4.0 features. If it does, you will need to rewrite those bits of your code to work with .net 3.5.

Targeting a particular .net version is trivial - it's done by choosing the version from a drop-down list in your project properties.

Jason Williams
_'You can retarget a .net 4.0 app down to 3.5 iff it doesn't use any .net 4.0 features.'_ This really means, 'doesn't use any features that aren't simply compiler tricks'. I'll get into trouble for calling them tricks, but oh well. A simple example is optional parameters in C#.
Marc
thank - please see my edit
Yaron Naveh
@Yaron: I would expect that each assembly should be able to independently "link" to a different version of the CLR. However, I can't confirm that, as I don't *know* the answer. Why not just try it and see?
Jason Williams
A: 

Note for your particular situation, you can cause the 3rd party application to load version 4.0 of the framework using the <requiredRuntime> configuration element. This will cause version 4.0 of the framework to load and hence your DLL can use 4.0 features.

Additionally, .NET 4.0 comes with a feature called In-Process Side-by-Site. I've never looked into this feature myself, but it might be useful for your situation as well...

Dean Harding