Skip to content

Commit 7e0ccfb

Browse files
authored
Merge pull request #21 from hydrostack/20-fix-compiler-warning-cs4014
Change Expression<Action> to LambdaExpression for handlers
2 parents afcaab2 + a4d78b1 commit 7e0ccfb

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/ExpressionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ internal static class ExpressionExtensions
77
private const string JsIndicationStart = "HYDRO_JS(";
88
private const string JsIndicationEnd = ")HYDRO_JS";
99

10-
public static (string Name, IDictionary<string, object> Parameters)? GetNameAndParameters(this Expression<Action> expression)
10+
public static (string Name, IDictionary<string, object> Parameters)? GetNameAndParameters(this LambdaExpression expression)
1111
{
1212
if (expression is not { Body: MethodCallExpression methodCall })
1313
{
@@ -40,7 +40,7 @@ internal static object EvaluateExpressionValue(Expression expression)
4040
{
4141
case ConstantExpression constantExpression:
4242
return constantExpression.Value;
43-
43+
4444
case MemberExpression memberExpression:
4545
var objectMember = Expression.Convert(memberExpression, typeof(object));
4646
var getterLambda = Expression.Lambda<Func<object>>(objectMember);

src/TagHelpers/HydroOnTagHelper.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public sealed class HydroOnTagHelper : TagHelper
1616
{
1717
private const string HandlersPrefix = "hydro-on:";
1818

19-
private IDictionary<string, Expression<Action>> _handlers;
19+
private IDictionary<string, Expression> _handlers;
2020

2121
/// <summary />
2222
[HtmlAttributeName(DictionaryAttributePrefix = HandlersPrefix)]
23-
public IDictionary<string, Expression<Action>> Handlers
23+
public IDictionary<string, Expression> Handlers
2424
{
25-
get => _handlers ??= new Dictionary<string, Expression<Action>>(StringComparer.OrdinalIgnoreCase);
25+
get => _handlers ??= new Dictionary<string, Expression>(StringComparer.OrdinalIgnoreCase);
2626
set => _handlers = value;
2727
}
2828

@@ -52,7 +52,12 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
5252

5353
foreach (var eventItem in _handlers)
5454
{
55-
var jsExpression = GetJsExpression(eventItem.Value);
55+
if (eventItem.Value is not LambdaExpression actionExpression)
56+
{
57+
throw new InvalidOperationException($"Wrong event handler statement in component for {modelType.Namespace}");
58+
}
59+
60+
var jsExpression = GetJsExpression(actionExpression);
5661

5762
if (jsExpression == null)
5863
{
@@ -70,7 +75,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
7075
}
7176
}
7277

73-
private static string GetJsExpression(Expression<Action> expression)
78+
private static string GetJsExpression(LambdaExpression expression)
7479
{
7580
var clientAction = GetJsClientActionExpression(expression);
7681

@@ -82,7 +87,7 @@ private static string GetJsExpression(Expression<Action> expression)
8287
return GetJsInvokeExpression(expression);
8388
}
8489

85-
private static string GetJsClientActionExpression(Expression<Action> expression)
90+
private static string GetJsClientActionExpression(LambdaExpression expression)
8691
{
8792
if (expression is not { Body: MethodCallExpression methodCall }
8893
|| methodCall.Method.DeclaringType != typeof(HydroClientActions))
@@ -101,7 +106,7 @@ private static string GetJsClientActionExpression(Expression<Action> expression)
101106
}
102107
}
103108

104-
private static string GetJsInvokeExpression(Expression<Action> expression)
109+
private static string GetJsInvokeExpression(LambdaExpression expression)
105110
{
106111
var eventData = expression.GetNameAndParameters();
107112

0 commit comments

Comments
 (0)