与执行组件相比,使用更新的.NET框架编译的负载组件

发布于 2025-01-29 02:16:44 字数 1312 浏览 4 评论 0原文

我有一个使用.net Framework 4.0编译的应用程序,我想在其中加载WPF USERCONTROL。 用户控件是.netFramework 4.8的DLL。

我一直在使用Console应用程序进行一些测试,并使用反射从.NETFRAMEWORK 4.0 DLL加载。 DLL与.netFrameWork 4.8编译,例如使用.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);
}

类库dll dll 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);
        }
    }
}

在此示例中,似乎一切正常,在运行时毫无问题。 这是允许的还是我会遇到一些问题?我认为这将失败,因为执行或调用组件是在.NET Framework 4.0中编译的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东风软 2025-02-05 02:16:44

这是允许的还是我会遇到一些问题?

是的。每个.NET框架版本高于4.0,都是4.0的就地升级。每个较高版本都完全替换了机器上的版本。

您仍然可以带有类库的 target .net Framework 4.0,但是所有这些都可以指定您正在使用.NET Framework 4.0的 api 。但是,如果您正在运行最新的.NET Framework Runtime ,它将运行最新版本的.NET Framework。基本上,它进入了一个兼容模式,该模式保留了.NET Framework 4.0行为,但实际上是在较新的.NET Framework版本上运行的。

至于是否 target .net框架4.0,这是另一回事。虽然Microsoft不再支持.NET Framework 4.0,但绝对没有理由无法针对.NET Framework 4.0,只要使用它的应用程序使用了仍然支持的.NET Framework版本。主要区别在于,出于何种原因而被困在旧版本上的用户仍然能够使用您的类库,如果您针对的是最新版本,则粘在.NET Framework 4.0上(并且有很多可能发生这种情况的可能原因)可以到您的图书馆。

话虽如此,不建议运行针对不支持的.NET框架版本的应用程序。有关仍然支持的.NET框架版本的列表,请参见 httpps ://learn.microsoft.com/en-us/lifecycle/faq/dotnet-framework 。这似乎与我刚才所说的相矛盾,但是 targinaling .net Framework 4.0 vs 运行 .net Framework 4.0。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文