Skip to content

Commit 3ebe107

Browse files
csharpfritzCopilot
andcommitted
fix: handle EnumParameter<T> in Style.FromAttributes reflection
Style.FromAttributes used Convert.ChangeType which cannot convert strings to EnumParameter<T>. Added pattern match for generic EnumParameter<> types that parses the underlying enum and creates the wrapper via Activator.CreateInstance. Also removed unnecessary Convert.ChangeType call since all branches now produce correctly typed values. Fixes: DataList InlineHeaderStyle tests (2 failures on CI) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ba79088 commit 3ebe107

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/BlazorWebFormsComponents/Style.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ internal Style() { }
3131

3232
public override string ToString() => this.ToStyle().NullIfEmpty();
3333

34+
private static object ParseEnumParameter(Type enumParamType, string value)
35+
{
36+
var enumType = enumParamType.GetGenericArguments()[0];
37+
var enumValue = Enum.Parse(enumType, value, ignoreCase: true);
38+
return Activator.CreateInstance(enumParamType, enumValue);
39+
}
40+
3441
public void FromUnknownAttributes(Dictionary<string, object> attributes, string prefix)
3542
{
3643

@@ -64,10 +71,12 @@ private void FromAttributes(IEnumerable<KeyValuePair<string, object>> attributes
6471
{ Name: nameof(Unit) } => new Unit(itemStyle.Value.ToString()),
6572
{ Name: nameof(FontUnit) } => FontUnit.Parse(itemStyle.Value.ToString()),
6673
{ IsEnum: true } => Enum.Parse(propInfo.PropertyType, itemStyle.Value.ToString()),
74+
var t when t.IsGenericType && t.GetGenericTypeDefinition() == typeof(EnumParameter<>)
75+
=> ParseEnumParameter(t, itemStyle.Value.ToString()),
6776
_ => itemStyle.Value
6877
};
6978

70-
propInfo.SetValue(this, Convert.ChangeType(outValue, propInfo.PropertyType));
79+
propInfo.SetValue(this, outValue);
7180

7281
}
7382
}

0 commit comments

Comments
 (0)