Skip to content

Commit 170c697

Browse files
committed
Add GetNamedArgument extension and refactor usage
Introduced a generic GetNamedArgument<T> extension method for AttributeData to simplify retrieval of named arguments. Refactored ReactiveCommandGenerator.Execute.cs to use the new method for accessing the AccessModifier argument.
1 parent 62bcf56 commit 170c697

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/ReactiveUI.SourceGenerators.Roslyn/Core/Extensions/AttributeDataExtensions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// The ReactiveUI and contributors licenses this file to you under the MIT license.
44
// See the LICENSE file in the project root for full license information.
55

6+
using System;
67
using System.Collections.Generic;
78
using System.Collections.Immutable;
89
using System.Linq;
@@ -44,6 +45,26 @@ public static bool TryGetNamedArgument<T>(this AttributeData attributeData, stri
4445
return false;
4546
}
4647

48+
/// <summary>
49+
/// Gets the named argument.
50+
/// </summary>
51+
/// <typeparam name="T">The type of argument to get.</typeparam>
52+
/// <param name="attributeData">The attribute data.</param>
53+
/// <param name="name">The name.</param>
54+
/// <returns>The named argument value.</returns>
55+
public static T? GetNamedArgument<T>(this AttributeData attributeData, string name)
56+
{
57+
foreach (var properties in attributeData.NamedArguments)
58+
{
59+
if (properties.Key == name)
60+
{
61+
return (T?)properties.Value.Value;
62+
}
63+
}
64+
65+
return default;
66+
}
67+
4768
/// <summary>
4869
/// Enumerates all items in a flattened sequence of constructor arguments for a given <see cref="AttributeData"/> instance.
4970
/// </summary>

src/ReactiveUI.SourceGenerators.Roslyn/ReactiveCommand/ReactiveCommandGenerator.Execute.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ public partial class ReactiveCommandGenerator
9292
token.ThrowIfCancellationRequested();
9393

9494
// Get AccessModifier enum value from the attribute
95-
attributeData.TryGetNamedArgument("AccessModifier", out int propertyAccessModifier);
96-
var accessModifier = propertyAccessModifier switch
95+
var accessModifier = attributeData.GetNamedArgument<int>("AccessModifier") switch
9796
{
9897
1 => "protected",
9998
2 => "internal",

0 commit comments

Comments
 (0)