简体   繁体   中英

Compile identical C# code for UAP and .NET4.5

I have a project that I want to be able to use as a nuget package in both a UWP/UAP10.0 project and a .NET4.5 Web API project (or any other type that uses a regular Class Library.

I have ported my original .NET4.5 code into a Portable Class Library to be able to package it up in nuget and use it in a UWP environment. My issue now is that I can't use the same dll in a regular .NET4.5 application from the nuget package (same dll, multiple lib targets).

I'm currently compiling and packaging two projects in Visual Studio: One portable class library, one Class Library.

This is not efficient at all as I have exact duplicates of code. Is there a way I can maintain/build one project into multiple Class Libraries for the nuget package? Or have another project that contains the identical code, referenced by both the Portable Class Library and the Class Library?

Here's what I'm doing in my nuspec at the moment:

  <file src="MyApp\bin\Release\MyApp.dll" target="lib\net45\MyApp.dll" />

  <!-- .NET 4.5.2 -->
  <file src="MyApp\bin\Release\MyApp.dll" target="lib\net452\MyApp.dll" />

  <!-- .NET 4.6 -->
  <file src="MyApp\bin\Release\MyApp.dll" target="lib\net46\MyApp.dll" />

  <!-- UAP -->
  <file src="MyApp.Portable\bin\Release\MyApp.Portable.dll" target="lib\uap10.0\MyApp-Uap10.dll" />

Well, the simplest way to keep your existing structure but to avoid having multiple copies of the C# source files on disk would be to simply add links to the existing files instead of adding copies .

Here's how.

First decide which project is the "master". This should have all (or at least the majority) of the files in it, as normal source code files. From your description you can probably just decide on which of the projects you already have to use for this.

Then, in those other projects first make sure you don't have local changes to the files (see further down if you do) . If you don't, go ahead with the following:

  1. Remove the files from the other project
  2. Re-add using the "Add existing item" in the project that is now empty but in the file selection dialog use the dropdown on the "Add" button and select "Add as link". Select files from the folder of the master project and use this function.

This will now link the other (non-master) projects to the single set of source code that is part of the master project.


What to do if you have local changes?

Invariably you will find that you need some local changes, a class available on one platform is not on another, or you need slightly different code. A typical issue is that some constructors might be missing on different platforms.

In this case you would set up a set of conditional definitions in the projects and use #if directives to handle this. Hopefully you don't have massive differences or this will make your code a mess. If you can use it, this would make the code look like this:

#if UAP
... code for the UAP platform here
#else
... code for all the other platforms here
#endif

Additionally there is a new project type now, for .NET Standard, and this single project type can target multiple platforms at the same time.

To use this follow this procedure:

  1. Create a new .NET Standard project, and configure it to target the specific .NET Standard level you require for your cross-platform usage
  2. Edit the .csproj file in a text editor (you can do this inside Visual Studio 2017 by the way)
  3. Find the line that looks like this:

     <TargetFramework>netstandard1.3</TargetFramework> 

    and change it to this:

     <TargetFrameworks>netstandard1.3;net45;net46</TargetFrameworks> 

    Note the plural change to the xml node here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM