简体   繁体   English

创建在 .net3.5 和 .net4.0 上兼容的 c# .dll

[英]create c# .dll compatible on both .net3.5 and .net4.0

I have to create a dll written on c#.我必须创建一个用 c# 编写的 dll。 The dll will have to be used on two different types on machines: -MachineA that has got .net35 installed, but .net40 is not installed -MachineB that has bot .net40 installed, but .net35 is not installed Currently there is no option to install .net35 on MachineB, or to install .net40 on MachineA.该 dll 必须在机器上的两种不同类型上使用: -MachineA 已安装 .net35,但未安装 .net40 -MachineB 已安装 bot .net40,但未安装 .net35 目前没有选项在 MachineB 上安装 .net35,或在 MachineA 上安装 .net40。

I would assume that creating a dll on .net35 would be able to be run on MachineB.我假设在 .net35 上创建一个 dll 将能够在 MachineB 上运行。 But when I try to call any of the dll's functions, I get an error saying that the application needs .net35 to be installed但是当我尝试调用 dll 的任何函数时,我收到一条错误消息,指出应用程序需要安装 .net35

So the question is this: Is it possible to create the dll in a way that it will be compatible on both MachineA and MachineB?所以问题是:是否有可能以在 MachineA 和 MachineB 上都兼容的方式创建 dll?

You can configure your app to run on both versions of .NET (3.5 and 4.0) by adding multiple supportedRuntime values in app.config file您可以通过在app.config文件中添加多个supportedRuntime值,将您的应用程序配置为在两个版本的 .NET(3.5 和 4.0)上运行

<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> 
    <supportedRuntime version="v2.0.50727"/>
  </startup>
</configuration>

.NET Framework 4 and later uses v4.0 runtime, .NET Framework 2.0, 3.0, and 3.5 use v2.0.50727 runtime. .NET Framework 4 及更高版本使用v4.0运行时,.NET Framework 2.0、3.0 和 3.5 使用v2.0.50727运行时。 Also, since you are targeting old .NET 3.5, it'll make sense to enable old runtime activation policy , useLegacyV2RuntimeActivationPolicy="true" .此外,由于您的目标是旧的 .NET 3.5,因此启用旧的运行时激活策略useLegacyV2RuntimeActivationPolicy="true"

The configuration above runs your code on .NET 3.5, if only this version is installed.如果仅安装了此版本,则上述配置在 .NET 3.5 上运行您的代码。 In case of both versions installed it'll will run on .NET 4.x如果安装了两个版本,它将在 .NET 4.x 上运行

Yo can also have a look at this MSDN article for more details您还可以查看这篇 MSDN 文章以了解更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM