Can you use a .NET 4.0 dll in a 3.5 project?
Asked
Active
Viewed 1.9k times
30
-
11Is anything ever forward-compatible? – ChaosPandion Sep 24 '10 at 21:21
-
3You can if you don't mind creating a COM wrapper/adapter and calling the .NET 4 DLL through that http://stackoverflow.com/a/9508452/74585 – Matthew Lock Feb 29 '12 at 23:50
-
Please check my other answer to the same question: http://stackoverflow.com/questions/16038442/how-can-i-use-net-4-0-code-in-c-sharp-project-which-is-built-using-net-framewo/16040309#16040309 – Aidin Apr 17 '13 at 19:58
3 Answers
26
Nope. You can use a .Net 3.5 assembly in a 4.0 project, but not the other way around.

NotMe
- 87,343
- 27
- 171
- 245
17
No you can't. An assembly compiled against .NET 4.0 can be loaded only by the CLR 4.0. On the other hand the CLR 4.0 can load assemblies compiled against .NET 3.5.

Darin Dimitrov
- 1,023,142
- 271
- 3,287
- 2,928
-
CLR 4.0 loading a 3.5 assembly surprised and confused me the other day. Scratched my head over that one for a bit. – Jim Mischel Sep 24 '10 at 21:29
-
1@Jim, why? If this wasn't possible I cannot even imagine ever migrating to .NET 4.0. Microsoft have always done a good job at keeping backwards compatibility when releasing new versions of the framework/CLR. – Darin Dimitrov Sep 24 '10 at 21:36
-
I don't know why it surprised me. In retrospect, it shouldn't have. But for some reason at the time I didn't expect it. – Jim Mischel Sep 24 '10 at 21:41
0
https://code.msdn.microsoft.com/Using-a-NET-4-Based-DLL-bb141db3/
Use our .NET 4 DLL via COM
using System; using Net4ToNet2Adapter; namespace Net2Assembly { class Program { static void Main(string[] args) { Console.WriteLine("CLR version from EXE: {0}", Environment.Version); Type myClassAdapterType = Type.GetTypeFromProgID("Net4ToNet2Adapter.MyClassAdapter"); object myClassAdapterInstance = Activator.CreateInstance(myClassAdapterType); IMyClassAdapter myClassAdapter = (IMyClassAdapter)myClassAdapterInstance; myClassAdapter.DoNet4Action(); } } }

Bugs
- 4,491
- 9
- 32
- 41
-
Why is this not the accepted answer? I haven't tested it yet, but I will tomorrow. – Arlen Beiler Nov 19 '19 at 02:57