创建一个在.NET 3.5和.NET 4.0上都兼容的C# .dll。

huangapple go评论83阅读模式
英文:

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

问题

I have to create a dll written on c#. 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.

I would assume that creating a dll on .net35 would be able to be run on 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

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?

英文:

I have to create a dll written on c#. 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.

I would assume that creating a dll on .net35 would be able to be run on 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

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?

答案1

得分: 2

你可以通过在 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 及更高版本使用 v4.0 运行时,而 .NET Framework 2.0、3.0 和 3.5 使用 v2.0.50727 运行时。另外,由于你的目标是旧版 .NET 3.5,启用 旧运行时激活策略 useLegacyV2RuntimeActivationPolicy="true" 是有意义的。

上述配置会在 .NET 3.5 上运行你的代码,如果两个版本都安装了,它将在 .NET 4.x 上运行。

你也可以查看这篇 MSDN 文章 获取更多详情。

英文:

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

&lt;configuration&gt;
  &lt;startup useLegacyV2RuntimeActivationPolicy=&quot;true&quot;&gt;
    &lt;supportedRuntime version=&quot;v4.0&quot; sku=&quot;.NETFramework,Version=v4.0&quot;/&gt; 
    &lt;supportedRuntime version=&quot;v2.0.50727&quot;/&gt;
  &lt;/startup&gt;
&lt;/configuration&gt;

.NET Framework 4 and later uses v4.0 runtime, .NET Framework 2.0, 3.0, and 3.5 use v2.0.50727 runtime. Also, since you are targeting old .NET 3.5, it'll make sense to enable old runtime activation policy, useLegacyV2RuntimeActivationPolicy=&quot;true&quot;.

The configuration above runs your code on .NET 3.5, if only this version is installed. In case of both versions installed it'll will run on .NET 4.x

Yo can also have a look at this MSDN article for more details

huangapple
  • 本文由 发表于 2020年1月6日 17:19:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定