Skip to content

Commit 753c664

Browse files
committed
优化从 LaunchSettings 读取逻辑
1 parent f77623f commit 753c664

1 file changed

Lines changed: 32 additions & 16 deletions

File tree

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
4+
using System.Runtime.Serialization;
35
using Newtonsoft.Json;
4-
using Newtonsoft.Json.Linq;
56

67
namespace UsingMSBuildCopyOutputFileToFastDebug
78
{
@@ -10,8 +11,7 @@ public class LaunchSettingsParser
1011
public bool Execute()
1112
{
1213
var file = Path.Combine("Properties", "launchSettings.json");
13-
Console.WriteLine("开始从 launchSettings 文件读取输出文件夹");
14-
Console.WriteLine($"开始读取{file}文件");
14+
Console.WriteLine($"开始从 launchSettings({file}) 文件读取输出文件夹");
1515
if (!File.Exists(file))
1616
{
1717
Console.WriteLine($"找不到{file}文件,读取结束");
@@ -20,25 +20,18 @@ public bool Execute()
2020

2121
var text = File.ReadAllText(file);
2222

23-
var root = (JObject) JsonConvert.DeserializeObject(text);
24-
if (root == null)
23+
var launchSettings = JsonConvert.DeserializeObject<LaunchSettings>(text);
24+
if (launchSettings == null)
2525
{
2626
return false;
2727
}
2828

29-
var profilesObject = (JObject)root["profiles"];
30-
31-
if (profilesObject == null)
29+
foreach (var launchSettingsProfile in launchSettings.Profiles)
3230
{
33-
return false;
34-
}
35-
36-
foreach (var keyValuePair in profilesObject)
37-
{
38-
var commandName = keyValuePair.Value?["commandName"];
39-
if (commandName?.ToString() == "Executable")
31+
var launchProfile = launchSettingsProfile.Value;
32+
if (launchProfile.CommandName == "Executable")
4033
{
41-
var executablePath = keyValuePair.Value["executablePath"];
34+
var executablePath = launchProfile.ExecutablePath;
4235
if (executablePath != null)
4336
{
4437
// executablePath = C:\lindexi\foo\foo.exe
@@ -53,4 +46,27 @@ public bool Execute()
5346

5447
public string LaunchMainProjectExecutablePath { set; get; }
5548
}
49+
50+
[DataContract]
51+
public class LaunchSettings
52+
{
53+
[DataMember(Name = "profiles")]
54+
public IDictionary<string, LaunchProfile> Profiles { get; set; }
55+
}
56+
57+
[DataContract]
58+
public class LaunchProfile
59+
{
60+
[DataMember(Name = "commandName")]
61+
public string CommandName { get; set; }
62+
63+
[DataMember(Name = "executablePath")]
64+
public string ExecutablePath { get; set; }
65+
66+
[DataMember(Name = "commandLineArgs")]
67+
public string CommandLineArgs { get; set; }
68+
69+
[DataMember(Name = "nativeDebugging")]
70+
public bool NativeDebugging { get; set; }
71+
}
5672
}

0 commit comments

Comments
 (0)