gpt4 book ai didi

c# - 如何在 MS 构建任务中使用 NewtonSoft.json?

转载 作者:行者123 更新时间:2023-11-30 20:28:04 25 4
gpt4 key购买 nike

我有一个构建任务,我想在其中使用 newtonsoft.json 进行一些 json 序列化/反序列化,如下所示:

<UsingTask TaskName="ReplaceFileText" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<TargetFilename ParameterType="System.String" Required="true" />
<SourceFilename ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Core" />

<Using Namespace="System" />
<Using Namespace="System.IO" />

<Code Type="Fragment" Language="cs">
<![CDATA[

string sourceJsonString = File.ReadAllText(SourceFilename);
string targetJsonString = File.ReadAllText(TargetFilename);

dynamic sourceJson = JsonConvert.DeserializeObject(sourceJsonString);
dynamic targetJson = JsonConvert.DeserializeObject(targetJsonString);

targetJson.firstProperty = sourceJson.firstProperty;
targetJson.secondProperty = sourceJson.secondProperty;


targetJsonString = JsonConvert.SerializeObject(targetJson, Formatting.Indented);

File.WriteAllText(targetFilename, targetJsonString);

]]>
</Code>
</Task>
</UsingTask>

<Target Name="TransformsWithStaging" BeforeTargets="Build">
<ReplaceFileText TargetFilename="$(ProjectDir)appsettings.json" SourceFilename="$(ProjectDir)appsettings.Staging.json" />
</Target>

上面的代码不起作用,因为我在上面的任务中使用了 NewtonSoft.Json,这个构建任务中没有引用它。

如何在构建任务中导入或引用 NewtonSoft.Json。我正在使用 Visual Studio 2017 Professional、.NET Core 2.0 并且项目是 WebApi 应用程序?

最佳答案

得到它与以下工作:

<UsingTask TaskName="ReplaceFileText" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<TargetFilename ParameterType="System.String" Required="true" />
<SourceFilename ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Core" />
<Reference Include="System.Runtime" />
<Reference Include="System.Dynamic.Runtime" />
<Reference Include="$(NugetPackageRoot)\microsoft.netcore.app\2.0.0\ref\netcoreapp2.0\System.ObjectModel.dll" />
<Reference Include="$(NugetPackageRoot)\newtonsoft.json\10.0.3\lib\netstandard1.3\Newtonsoft.Json.dll" />
<Reference Include="$(NugetPackageRoot)\system.runtime.serialization.primitives\4.1.1\lib\netstandard1.3\System.Runtime.Serialization.Primitives.dll" />

<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Collections.Specialized" />
<Using Namespace="Newtonsoft.Json" />
<Using Namespace="Newtonsoft.Json.Linq" />

<Code Type="Fragment" Language="cs">
<![CDATA[

string sourceJsonString = File.ReadAllText(SourceFilename);
string targetJsonString = File.ReadAllText(TargetFilename);

JObject sourceJsonObject = JObject.Parse(sourceJsonString);
JObject targetJsonObject = JObject.Parse(targetJsonString);

targetJsonObject["someProperty"] = sourceJsonObject["someProperty"];

File.WriteAllText(TargetFilename, targetJsonObject.ToString());

]]>
</Code>
</Task>
</UsingTask>

<Target Name="TransformsWithDevelopment" Condition="'$(Configuration)'=='Development'" BeforeTargets="Build">
<ReplaceFileText TargetFilename="$(ProjectDir)appsettings.json" SourceFilename="$(ProjectDir)appsettings.Development.json" />
</Target>

<Target Name="TransformsWithStaging" Condition="'$(Configuration)'=='Staging'" BeforeTargets="Build">
<ReplaceFileText TargetFilename="$(ProjectDir)appsettings.json" SourceFilename="$(ProjectDir)appsettings.Staging.json" />
</Target>

<Target Name="TransformsWithProduction" Condition="'$(Configuration)'=='Production'" BeforeTargets="Build">
<ReplaceFileText TargetFilename="$(ProjectDir)appsettings.json" SourceFilename="$(ProjectDir)appsettings.Production.json" />
</Target>

关于c# - 如何在 MS 构建任务中使用 NewtonSoft.json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47869999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com