Using the. NET Framework 2.0 assembly in programs in the. NET Framework 4.0

Source: Internet
Author: User

Scene

In programs that target the framework for . NET Framework 4 , reference the target framework for the . NET Framework 2.0 assemblies and use the types in the . NET Framework 2.0 assemblies or Static methods.

Problem

In general, the following error is reported

Unhandled exception: System.IO.FileLoadException: The mixed-mode assembly was generated for the runtime of the "v2.0.50727" version and cannot be loaded in the 4.0 runtime without additional information being configured.

Resolving document references

The section on configuring file schemas in the documentation for the. NET Framework has a workaround for the above issues. See useLegacyV2RuntimeActivationPolicy the description of attributes in the <startup> element.

Simply put,

You should modify the program configuration file when you need and have to reference the target framework for the . NET Framework 2.0 assembly in a program that targets the framework for. NET Framework 4 .

In general, the program configuration file is named "app. config" in the project root directory. The default content is as follows:

<?xml version="1.0"?><configuration>  <startup>    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>  </startup></configuration>

If you don't have it in your project, you can add it manually and add one to the "csproj" file:

<ItemGroup>  <None Include="app.config" /></ItemGroup>

Depending on the <startup> element, you need to modify the "app. Config" profile to:

<?xml version="1.0"?><configuration>  <startup useLegacyV2RuntimeActivationPolicy="true">    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>  </startup></configuration>

You can reference the assemblies in your program that target the framework for the . NET Framework 2.0 .

Publishing matters

When the program is release, the "Xxx.exe.config" file is generated in the Release folder, which is the program's configuration file. If you do not use the default configuration, the file should be published with the executable program and remain in the same folder at the time of publication. Otherwise, the program will use the default configuration.

Other

For more details on the program's configuration file, refer to the. NET Framework's configuration file schema.

Using the. NET Framework 2.0 assembly in programs in the. NET Framework 4.0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.