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