Search code examples
c#reflectionsystem.reflection.net-framework-version.net-4.8

Load Assembly compiled with newer .Net Framework than executing assembly


I have an application compiled with .Net Framework 4.0 and I want to load a WPF UserControl inside it. The user control is a dll compiled for .NetFramework 4.8.

I have been doing some tests with Console Application and I have loaded from .NetFramework 4.0 a dll using reflection. The dll is compiled with .NetFramework 4.8 and using for example the RSA constructor introduced in .NetFramework 4.7

Console App targeting .NetFramework 4.0

static void Main()
{
    var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "DependencyDll.dll");
    var assembly = Assembly.LoadFile(path);
    var type = assembly.GetType("Crypto");
    var typeInstance = Activator.CreateInstance(type);

    var method = type.GetMethod("Encrypt");
    var text = (string) method.Invoke(typeInstance, new object[]{"Hellooooo"});
    Console.WriteLine(text);
}

Class library Dll targeting .NetFramework 4.8

public class Crypto
{
    public string Encrypt(string text)
    {
        using(var rsa = RSA.Create(2048))
        {
            var bytes = rsa.Encrypt(Encoding.UTF8.GetBytes(text), RSAEncryptionPadding.OaepSHA512);
            var decrypt = rsa.Decrypt(bytes, RSAEncryptionPadding.OaepSHA512);
            return Encoding.UTF8.GetString(decrypt);
        }
    }
}

In this example it seems that everything works well with no problem at runtime. Is this permited or maybe I will have some problem? I thougth that this will fail due the executing or calling assembly was compiled in .Net Framework 4.0.


Solution

  • Is this permited or maybe I will have some problem?

    Yes. Every .NET Framework version higher than 4.0 is an in-place upgrade of 4.0. Each higher version completely replaces the version before it on the machine.

    You can still target .NET Framework 4.0 with a class library, but all that does is specify that you are using the API of .NET Framework 4.0. However, if you are running a more recent .NET Framework runtime, it will run the more recent version of .NET Framework. Basically, it goes into a compatibility mode that preserves the .NET Framework 4.0 behavior, but it is actually running on the newer .NET Framework version.

    As for whether to target .NET Framework 4.0, that is a different issue. While it is true that Microsoft no longer supports .NET Framework 4.0, there is absolutely no reason why you cannot target .NET Framework 4.0 as long as the application that uses it is using a .NET Framework version that is still supported. The primary difference is that users who are stuck on the old version for whatever reason will still be able to use your class library, where if you target a more recent version, those stuck on .NET Framework 4.0 (and there are plenty of possible reasons why this could occur) won't be able to your library.

    That being said, it is not recommended to run an application that targets an unsupported .NET Framework version. For a list of the .NET Framework versions that are still supported, see https://learn.microsoft.com/en-us/lifecycle/faq/dotnet-framework. This may seem to contradict what I just said, but there is a difference between targeting .NET Framework 4.0 vs running .NET Framework 4.0.