Skip to content

Handle custom attributes from attached property setter #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/XamlX/Ast/Clr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ public XamlAstClrProperty(IXamlLineInfo lineInfo, IXamlProperty property,
}
}

public XamlAstClrProperty(IXamlLineInfo lineInfo, string name, IXamlType declaringType,
IXamlMethod? getter, IEnumerable<IXamlPropertySetter>? setters) : base(lineInfo)
public XamlAstClrProperty(
IXamlLineInfo lineInfo,
string name,
IXamlType declaringType,
IXamlMethod? getter,
IEnumerable<IXamlPropertySetter>? setters,
IEnumerable<IXamlCustomAttribute>? customAttributes)
: base(lineInfo)
{
Name = name;
DeclaringType = declaringType;
Expand All @@ -83,13 +89,30 @@ public XamlAstClrProperty(IXamlLineInfo lineInfo, string name, IXamlType declari
IsFamily = getter?.IsFamily == true;
if (setters != null)
Setters.AddRange(setters);
if (customAttributes is not null)
CustomAttributes.AddRange(customAttributes);
}

public XamlAstClrProperty(IXamlLineInfo lineInfo, string name, IXamlType declaringType,
IXamlMethod? getter, params IXamlMethod?[] setters) : this(lineInfo, name, declaringType,
getter, setters.Where(x=> !(x is null)).Select(x => new XamlDirectCallPropertySetter(x!)))
public XamlAstClrProperty(
IXamlLineInfo lineInfo,
string name,
IXamlType declaringType,
IXamlMethod? getter,
IEnumerable<IXamlMethod?>? setters,
IEnumerable<IXamlCustomAttribute>? customAttributes)
: this(
lineInfo,
name,
declaringType,
getter,
setters?.Where(x => x is not null).Select(x => new XamlDirectCallPropertySetter(x!)),
customAttributes)
{
}

public XamlAstClrProperty(IXamlLineInfo lineInfo, string name, IXamlType declaringType, IXamlMethod? getter)
: this(lineInfo, name, declaringType, getter, (IEnumerable<IXamlPropertySetter>?)null, null)
{
}

public override string ToString() => DeclaringType.GetFqn() + "." + Name;
Expand Down
11 changes: 7 additions & 4 deletions src/XamlX/Transform/Transformers/PropertyReferenceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode nod
&& p.Add != null);
if (clrEvent != null)
return new XamlAstClrProperty(prop,
prop.Name, clrEvent.Add!.DeclaringType, null, clrEvent.Add);
prop.Name, clrEvent.Add!.DeclaringType, null, [clrEvent.Add], null);
}

// Look for attached properties on declaring type
Expand Down Expand Up @@ -68,16 +68,19 @@ public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode nod
}

if (setter != null || getter != null)
return new XamlAstClrProperty(prop, prop.Name, declaringType, getter, setter);
{
var customAttributes = setter?.GetParameterInfo(1).CustomAttributes;
return new XamlAstClrProperty(prop, prop.Name, declaringType, getter, [setter], customAttributes);
}

if (adder != null)
return new XamlAstClrProperty(prop, prop.Name, declaringType, null, adder);
return new XamlAstClrProperty(prop, prop.Name, declaringType, null, [adder], null);

return context.ReportTransformError(
$"Unable to resolve suitable regular or attached property {prop.Name} on type {declaringType.GetFqn()}",
node, FakeProperty());

XamlAstClrProperty FakeProperty() => new(prop, prop.Name, XamlPseudoType.Unknown, null, Array.Empty<IXamlMethod>());
XamlAstClrProperty FakeProperty() => new(prop, prop.Name, XamlPseudoType.Unknown, null);
}

return node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode nod
adders.Select(a => new XamlDirectCallPropertySetter(a)
{
BinderParameters = {AllowMultiple = true}
})),
}), null),
Array.Empty<IXamlAstValueNode>(),
false);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/XamlParserTests/DynamicSettersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ public SpecialProperty(
XamlAstClrProperty original,
IXamlMethod getSpecialHandlerMethod,
IXamlMethod handleMethod)
: base(original, original.Name, original.DeclaringType, original.Getter, original.Setters)
: base(original, original.Name, original.DeclaringType, original.Getter, original.Setters, null)
=> Setters.Add(new SpecialHandlerPropertySetter(
handleMethod.DeclaringType,
handleMethod.Parameters[0],
Expand Down