"The former. NET core era" how to implement cross-platform code reuse--source file reuse

Source: Internet
Author: User
Tags silverlight

Microsoft introduced the first version of the. NET framework in 2002, a framework that is primarily geared towards Windows desktop (Windows Forms) and server (ASP. Web forms). Since then, the dominance of the PC has been challenged and even replaced by other devices, and Microsoft has simplified and changed the. NET Framework based on the needs of the device itself, and has introduced the. NET Framework for specific device types, mainstream including windows Phones, Windows Store, Silverlight, Xbox, and more, and they support mobile, tablet, and gaming devices, respectively. Because these different. NET frameworks are completely independent, this makes it difficult to develop a "portable (portable)" application that supports multiple devices. At the same time, through the use of mono,. NET can be successfully ported to non-Windows platforms including Mac OS, Linux, IOS, Android and FreeBSD.

For the BCL (Basic Class Library) for each. NET framework platform, including mono, there are some similarities in the API definition level, but because they are defined in different assemblies, the PCL (Portal Class Library), sharing for assemblies is not possible, and we can only share them at the source level. The sharing of source code is achieved by sharing source files between different projects (for different. NET framework platforms), and we have three different options for how to use them in a specific way.

One, source file sharing

For a source file that can be shared with multiple projects for different. NET Framework platforms, there are many code definitions that are specific to a particular. NET Framework platform. For this kind of code, we need to write in the following way, the corresponding project to add a compilation of the way to choose the code compiled by the compiler to build the assembly.

   1:#if WINDOWS
   2:     << for Windows desktop>>
   3:#elif XBOX
   4:     << for xbox>>
   5:#elif windows_phone
   6:     << for Windows phone>>
   7:#else
   8:     << for other platforms >>
   9:#endif

If multiple project files for different. NET framework platforms exist in the same physical directory, source files that exist in the same directory can be included in these projects for sharing purposes. As shown, two projects for Silverlight and WPF share the same directory, and C # files in a directory with two project files Shared.cs can be included in these two projects at the same time.

Second, the file link

When we add an existing file to the current project in the default way, Visual studio copies the target file to the local directory of the project, so it is not shared at all. However, for the addition of existing files to support a way called "link" so that the files added to the project will still be the original address, we can add a link to the same file for multiple projects to achieve the source file cross-project sharing. Also shown above are two projects for Silverlight and WPF, regardless of the directory in which the project files and files need to be shared, we can share the Shared.cs file in the same way as the Add File link.

Third, Shared Project

The purpose of a project is generally to organize the source files and other related resources and eventually compile them into a deployable assembly. However, the project type of shared project is special, it only has the function of organizing the source file, but cannot build the assembly by compiling, it exists to realize the sharing of the source file. For the sharing of the two source codes described above, they are shared for a single file, and shared project can package multiple sources for bulk sharing.

As shown, we can create a shared project type Shared.shproj and add the three C # files (Foo.cs, Bar.cs, and Baz.cs) that need to be shared. We will add a reference to this project to a Silverlight project (silverlightapp.csproj) and Windows Phone Project (winphoneapp.csproj), when we compile these two projects, three C # files included in the project Shared.shproj are automatically compiled as source files for the current project.

"The former. NET core era" how to implement cross-platform code reuse--source file reuse

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.