C # DLL reference problem on different platforms

Source: Internet
Author: User

Many times, we need to refer to dll,32 bits (X86) and 64-bit (X64) under different platforms. If the platform is wrong, a BadImageFormatException exception is thrown in C # that fails to load or assembly.

Solution Ideas

We cannot add DLLs under different platforms at the same time, even if the names are not the same (the datagram type exists in two DLLs at the same time). Can it be referenced only on some platform and not in other platforms?

We know that the configuration of the VS project is saved in the configuration file (. csproj). In VS, find the appropriate item, right-click the Unload project, and when finished, the project turns gray, similar to. Then right-click on the item and check edit (project name). csproj. We can look at the VS engineering file. Such as:

In which we were pleasantly surprised to find condition= "' $ (Configuration) |$ (Platform) ' = = ' debug| AnyCPU ' ". We suspect that $ (Platform) is a specific platform, including AnyCPU, X86, X64. The condition represents the condition. We can verify this by modifying the configuration in Debug and release scenarios (which can be modified in the Build tab of the project's properties).

Looking further down the project file, we found:

This is the DLL we referenced in the VS project, so let's try to set condition in it, and the answer is yes.

Then we try to add different conditions on different platforms, such as:

After verification, we found that success ...

In 64-bit cases, in 32-bit cases.

Extended

condition except can use = =, can also use!=,<, <=, >=, and other relational operators,!,and,or and other logical operators, see MSBuild Conditions.

The VS Project file contains the properties (defined in PropertyGroup中) and itemgroups (defined in ItemGroup) two types of variables. $ (PropertyName) can refer to all definitions in the PropertyGroup中的变量,详见 simple introduction and use of MSBuild

At the bottom of the VS project file is a line of commented code, which is known as pre-build and post-build events

For more information about MSBuild, you can view MSDN-related documentation for your interest.

Platform and platform goals

Platform refers to the platform defined in Configuration Manager (located under the Build menu), which corresponds to the platform

The platform target is the target platform set in the project build properties, corresponding to the Platformtarget

Applications in C + +

Not tested in C + +, it should, in principle, be an entry added in the project file after adding a library directory in C + +.

Which friend has tested, please advise, grateful

C # DLL reference problem on different platforms

Related Article

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.