Skip to content

Commit 98afe71

Browse files
committed
kate.shared.CommandLine: v1.6.2
1 parent 748f7d2 commit 98afe71

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

kate.shared.CommandLine/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v1.6.2 (27th Oct, 2025)
2+
3+
- Fixed reflection bugs when setting default value for options.
4+
15
## v1.6.1 (27th Oct, 2025)
26

37
- Fixed non-string default values not working for options.

kate.shared.CommandLine/CommandLineHelper.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,23 +183,25 @@ string BuildShortName(ActionParameterAttribute a)
183183
}
184184
}
185185
}
186-
187186
void SetDefaultValue(
188187
PropertyInfo prop,
189188
Type argumentInstanceType,
190189
object argumentInstance)
191190
{
192-
var defaultValueAttr = prop.GetCustomAttribute<DefaultValueAttribute>();
193-
if (defaultValueAttr != null)
191+
var delMethod = typeof(CommandLineHelper).GetMethod(nameof(GetDefaultValueDelegate), BindingFlags.NonPublic | BindingFlags.Static);
192+
var delGen = delMethod.MakeGenericMethod(prop.PropertyType);
193+
var del = delGen.Invoke(null, new object[] { prop });
194+
if (del != null)
194195
{
195196
var defaultValueProperty = argumentInstanceType.GetProperty("DefaultValueFactory",
196197
BindingFlags.Public | BindingFlags.Instance);
197198
if (defaultValueProperty == null)
198199
throw new InvalidOperationException(
199200
$"Could not find property DefaultValueFactory on type {argumentInstanceType}");
200-
defaultValueProperty.SetValue(argumentInstance, (ArgumentResult _) => defaultValueAttr.Value);
201+
defaultValueProperty.SetValue(argumentInstance, del);
201202
}
202203
}
204+
203205
string[] GenerateAliases(
204206
PropertyInfo prop)
205207
{
@@ -438,5 +440,12 @@ private static string FormatTypeName(Type type)
438440
{
439441
return type.Namespace + '.' + type.Name;
440442
}
443+
444+
private static Func<ArgumentResult, T> GetDefaultValueDelegate<T>(PropertyInfo prop)
445+
{
446+
var defaultValueAttr = prop.GetCustomAttribute<DefaultValueAttribute>();
447+
if (defaultValueAttr == null) return null;
448+
return (ArgumentResult _) => (T)defaultValueAttr.Value;
449+
}
441450
}
442451
}

kate.shared.CommandLine/kate.shared.CommandLine.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>
55
<Nullable>disable</Nullable>
66
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
7-
<Version>1.6.1</Version>
7+
<Version>1.6.2</Version>
88

99
<Title>Kate's Shared Library - System.CommandLine Utilities</Title>
1010
<Copyright>Copyright 2022-2025 Kate Ward &lt;kate@dariox.club&gt;</Copyright>

0 commit comments

Comments
 (0)