问题
I have some codebase from which I want to build a .NET Core library and a UWP library. Initially, I wanted to build a single lib which would serve for both but I didn't find a way for this (in UWP case, the lib needs to reference UWP specific classes like StreamSocket).
So I created a separate project file for UWP. However, looks like I can't create a separate project.json (I can't have core.json and uwp.json because project.json name is constant). And I don't want to create multiple copies of my codebase (each one with its own project.json) in different folder, I'd rather keep a single copy.
Is it feasible somehow?
回答1:
The cleanest way to achieve this would be to have three libraries:
MyLib.Common
MyLib.Core
MyLib.Uwp
Keep all of the shared, platform-neutral code in MyLib.Common, and reference that from the two platform-specific projects. This way, you have little or no code duplication, and you can also reference UWP-specific libraries from the UWP project without any problems.
You might also be able to get two projects to reference the same .cs
files (and get rid of the shared project), but that seems more hackish to me.
回答2:
Well, it looks like UWP and .NET Core projects can use the same project.json. Its "frameworks" section looks like:
"frameworks": {
"netstandard1.3": {},
"uap10.0": {
"dependencies": {
"Microsoft.NETCore.UniversalWindowsPlatform": "5.2.2"
}
}
},
Because UWP project has its own csproj file, this leads to building 3 DLLs: project.json causes netstandard1.3 and uap10.0 folders appear in bin/Debug (and places resulting DLLs there) when building .NET Core project while UWP project itself builds yet another DLL right in bin/Debug.
bin/Debug/uap10.0/My.dll is garbage, it cannot contain any UWP-specific code (because Universal Windows reference is not available in .NET Core projects) and therefore I don't define WINDOWS_UWP in project.json's "uap10.0" part.
However, UWP csproj file does define WINDOWS_UWP, and Universal Windows is available in that project. So, when UWP project gets built, it in fact uses only this:
"uap10.0": {
"dependencies": {
"Microsoft.NETCore.UniversalWindowsPlatform": "5.2.2"
},
(if this code is missing in project.json, UWP project won't build). The rest of project.json (mostly specific to "netstandard1.3" target) makes no harm for the build process.
So I'm relying on the fact that I can define conditional directives (WINDOWS_UWP in this case) not only in project.json but in csproj file as well and thus achieve extra results when processing UWP project which uses the same project.json file as .NET Core project. To some extent, UWP csproj acts as another project.json which is merged with the real one during the building process.
Yes, I'm getting an extra DLL bin/Debug/uap10.0/My.dll which is of no use (just a waste of compiler time) but I'll then pick only those outputs which are useful for me so it's not a problem in my particular case.
来源:https://stackoverflow.com/questions/41146301/can-two-project-json-files-share-the-same-codebase