Can you use a .NET 4.0 dll in a 3.5 project?
- 11Is anything ever forward-compatible?ChaosPandion– ChaosPandion2010-09-24 21:21:04 +00:00Commented Sep 24, 2010 at 21:21
- 3You can if you don't mind creating a COM wrapper/adapter and calling the .NET 4 DLL through that stackoverflow.com/a/9508452/74585Matthew Lock– Matthew Lock2012-02-29 23:50:57 +00:00Commented Feb 29, 2012 at 23:50
- Please check my other answer to the same question: stackoverflow.com/questions/16038442/…Aidin– Aidin2013-04-17 19:58:53 +00:00Commented Apr 17, 2013 at 19:58
Add a comment |
3 Answers
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.
3 Comments
Jim Mischel
CLR 4.0 loading a 3.5 assembly surprised and confused me the other day. Scratched my head over that one for a bit.
Darin Dimitrov
@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.
Jim Mischel
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.
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(); } } } 1 Comment
Arlen Beiler
Why is this not the accepted answer? I haven't tested it yet, but I will tomorrow.