C# – How to load an older version of a .NET assembly

cnet

I have a WPF/C# application that references .NET 4.0 assemblies. However, within the application is a text editor that needs to display C# intellisense tied to .NET 3.5 assemblies. Thus, I want to be able to load the appropriate .NET 3.5 assemblies at runtime. However, when I try to use:

Assembly.Load()
Assembly.LoadFile()
Assembly.LoadFrom()

I always get the latest version from the GAC. Reading their MSDN pages, it seems like that's unfortunately by design.

I tried following the proposed solutions in this stack overflow post. However, the linked webpages' solutions don't work and a remoting process seems like overkill. Does anyone know a better and/or easier way to load older .NET assemblies at runtime?

Best Answer

By default, you cannot load assemblies made for previous versions of the .NET framework on the .NET framework version 4.

However, there is a configuration attribute that allows that: http://msdn.microsoft.com/en-us/library/bbx34a2h.aspx

Put the following lines in your app.config file:

<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true|false" > 
    </startup>
</configuration>

There are reasons why this is not active by default, but I believe that it should be appropriate for your use.