Skip to content

Commit 822132f

Browse files
committed
可以从用户设置启动的文件夹
1 parent 0961cde commit 822132f

36 files changed

Lines changed: 3199 additions & 9 deletions

Code/UsingMSBuildCopyOutputFileToFastDebug/Build/dotnetCampus.UsingMSBuildCopyOutputFileToFastDebug.targets

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
<NuGetUsingMSBuildCopyOutputFileToFastDebugTaskFolder Condition=" '$(MSBuildRuntimeType)' != 'Core'">$(MSBuildThisFileDirectory)..\tools\net48\</NuGetUsingMSBuildCopyOutputFileToFastDebugTaskFolder>
77
</PropertyGroup>
88

9-
10-
119
<UsingTask TaskName="UsingMSBuildCopyOutputFileToFastDebug.SafeOutputFileCopyTask"
1210
AssemblyFile="$(NuGetUsingMSBuildCopyOutputFileToFastDebugTaskFolder)\UsingMSBuildCopyOutputFileToFastDebug.dll" />
1311
<UsingTask TaskName="UsingMSBuildCopyOutputFileToFastDebug.LaunchSettingsParser"
1412
AssemblyFile="$(NuGetUsingMSBuildCopyOutputFileToFastDebugTaskFolder)\UsingMSBuildCopyOutputFileToFastDebug.dll" />
1513

16-
<Target Name="ParseLaunchSettings" AfterTargets="AfterBuild">
14+
<Target Name="ParseLaunchSettings" AfterTargets="AfterBuild"
15+
BeforeTargets="CopyOutputLibToFastDebug" Condition="$(MainProjectPath)==''">
1716
<Message Text="当前使用的复制方法 $(NuGetUsingMSBuildCopyOutputFileToFastDebugTaskFolder)" />
1817
<LaunchSettingsParser>
1918
<Output TaskParameter="LaunchMainProjectPath" PropertyName="LaunchMainProjectPath" ></Output>

Code/UsingMSBuildCopyOutputFileToFastDebug/LaunchSettingsParser.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Dynamic;
34
using System.Globalization;
45
using System.IO;
56
using System.Runtime.Serialization.Json;
67
using System.Xml;
8+
using Lsj.Util.Collections;
9+
using Lsj.Util.JSON;
710
using Microsoft.Build.Framework;
811
//using Newtonsoft.Json;
912
//using Newtonsoft.Json.Linq;
@@ -15,8 +18,6 @@ public class LaunchSettingsParser : Microsoft.Build.Utilities.Task
1518
/// <inheritdoc />
1619
public override bool Execute()
1720
{
18-
Debugger.Launch();
19-
2021
var file = Path.Combine("Properties", "launchSettings.json");
2122
Console.WriteLine("开始从 launchSettings 文件读取输出文件夹");
2223
Console.WriteLine($"开始读取{file}文件");
@@ -27,13 +28,49 @@ public override bool Execute()
2728

2829
var text = File.ReadAllText(file);
2930

31+
var o = JSONParser.Parse(text);
32+
var profiles = o.profiles;
33+
var data = profiles.data;
34+
if (data is SafeDictionary<string, object> d)
35+
{
36+
foreach (var keyValuePair in d)
37+
{
38+
if (keyValuePair.Value is JSONObject jsonObject)
39+
{
40+
if (jsonObject.TryGetMember(new CustomGetMemberBinder("ExecutablePath", true),
41+
out var filePath))
42+
{
43+
var path = filePath?.ToString();
3044

31-
//var jObject = JObject.Parse(File.ReadAllText(file));
45+
if (!string.IsNullOrEmpty(path))
46+
{
47+
LaunchMainProjectPath = Path.GetDirectoryName(path);
48+
Console.WriteLine($"读取到 {LaunchMainProjectPath} 文件夹");
49+
return true;
50+
}
51+
}
52+
}
53+
}
54+
}
3255

3356
return true;
3457
}
3558

3659
[Output]
3760
public string LaunchMainProjectPath { set; get; }
3861
}
62+
63+
public class CustomGetMemberBinder : GetMemberBinder
64+
{
65+
/// <inheritdoc />
66+
public CustomGetMemberBinder(string name, bool ignoreCase) : base(name, ignoreCase)
67+
{
68+
}
69+
70+
/// <inheritdoc />
71+
public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
72+
{
73+
return null;
74+
}
75+
}
3976
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
3+
namespace Lsj.Util.JSON
4+
{
5+
/// <summary>
6+
/// Custom Json Property Name Attribute
7+
/// </summary>
8+
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
9+
public class CustomJsonPropertyNameAttribute : Attribute
10+
{
11+
/// <summary>
12+
/// Json Property Name
13+
/// </summary>
14+
public string Name { get; set; }
15+
16+
/// <summary>
17+
///
18+
/// </summary>
19+
/// <param name="name"></param>
20+
public CustomJsonPropertyNameAttribute(string name)
21+
{
22+
this.Name = name;
23+
}
24+
}
25+
}
26+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
3+
namespace Lsj.Util.JSON
4+
{
5+
/// <summary>
6+
/// Custom Seriailize Attribute
7+
/// </summary>
8+
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
9+
public class CustomSerializeAttribute : Attribute
10+
{
11+
/// <summary>
12+
/// Serializer Type
13+
/// </summary>
14+
public Type Serializer { get; set; }
15+
/// <summary>
16+
/// Json Source Type
17+
/// </summary>
18+
public Type SourceType { get; set; }
19+
20+
/// <summary>
21+
///
22+
/// </summary>
23+
public CustomSerializeAttribute()
24+
{
25+
}
26+
}
27+
28+
/// <summary>
29+
/// ISerializer
30+
/// </summary>
31+
public interface ISerializer
32+
{
33+
/// <summary>
34+
/// Convert
35+
/// </summary>
36+
/// <param name="obj">object</param>
37+
/// <returns></returns>
38+
object Convert(object obj);
39+
40+
/// <summary>
41+
/// Parse
42+
/// </summary>
43+
/// <param name="str">json str</param>
44+
/// <returns></returns>
45+
object Parse(object str);
46+
}
47+
}
48+
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.CompilerServices;
4+
using Microsoft.CSharp.RuntimeBinder;
5+
using Lsj.Util.Text;
6+
7+
namespace Lsj.Util.Dynamic
8+
{
9+
/// <summary>
10+
/// Dynamic Helper
11+
/// </summary>
12+
public static class DynamicHelper
13+
{
14+
/// <summary>
15+
/// Get Member
16+
/// </summary>
17+
/// <param name="obj"></param>
18+
/// <param name="memberName">member name</param>
19+
/// <returns>value</returns>
20+
public static object GetMember(this object obj, string memberName)
21+
{
22+
var binder = Binder.GetMember(CSharpBinderFlags.None, memberName, obj.GetType(), new List<CSharpArgumentInfo> { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) });
23+
var callsite = CallSite<Func<CallSite, object, object>>.Create(binder);
24+
return callsite.Target(callsite, obj);
25+
}
26+
27+
/// <summary>
28+
/// Set Member
29+
/// </summary>
30+
/// <param name="obj"></param>
31+
/// <param name="memberName">member name</param>
32+
/// <param name="value">value</param>
33+
public static void SetMember(this object obj, string memberName, object value)
34+
{
35+
var binder = Binder.SetMember(CSharpBinderFlags.None, memberName, obj.GetType(), new List<CSharpArgumentInfo> { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) });
36+
var callsite = CallSite<Func<CallSite, object, object, object>>.Create(binder);
37+
callsite.Target(callsite, obj, value);
38+
}
39+
40+
/// <summary>
41+
/// Cast
42+
/// </summary>
43+
/// <typeparam name="T"></typeparam>
44+
/// <param name="obj"></param>
45+
/// <returns></returns>
46+
public static T Cast<T>(this object obj)
47+
{
48+
return (T)obj;
49+
}
50+
51+
/// <summary>
52+
/// Cast and Assign
53+
/// </summary>
54+
/// <param name="toAssign"></param>
55+
/// <param name="val"></param>
56+
/// <returns></returns>
57+
public static bool AutoCastAndAssign(ref string toAssign, object val)
58+
{
59+
toAssign = val.ToString();
60+
return true;
61+
}
62+
63+
/// <summary>
64+
/// Cast and Assign
65+
/// </summary>
66+
/// <param name="toAssign"></param>
67+
/// <param name="val"></param>
68+
/// <returns></returns>
69+
public static bool AutoCastAndAssign(ref int toAssign, object val)
70+
{
71+
if (val.IsNumeric())
72+
{
73+
toAssign = (int)val;
74+
return true;
75+
}
76+
else
77+
{
78+
var str = (val as object).ToString();
79+
var intVal = str.ConvertToIntWithNull();
80+
if (intVal != null)
81+
{
82+
toAssign = intVal.Value;
83+
return true;
84+
}
85+
else
86+
{
87+
return false;
88+
}
89+
}
90+
}
91+
92+
/// <summary>
93+
/// Cast and Assign
94+
/// </summary>
95+
/// <param name="toAssign"></param>
96+
/// <param name="val"></param>
97+
/// <returns></returns>
98+
public static bool AutoCastAndAssign(ref decimal toAssign, object val)
99+
{
100+
if (val.IsNumeric())
101+
{
102+
toAssign = (int)val;
103+
return true;
104+
}
105+
else
106+
{
107+
var str = (val as object).ToString();
108+
var decimalVal = str.ConvertToDecimalWithNull();
109+
if (decimalVal != null)
110+
{
111+
toAssign = decimalVal.Value;
112+
return true;
113+
}
114+
else
115+
{
116+
return false;
117+
}
118+
}
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)