Skip to content

Commit a57ac60

Browse files
committed
Handle custom attributes from attached property setter
1 parent ab84721 commit a57ac60

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

src/XamlX/Ast/Clr.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ public XamlAstClrProperty(IXamlLineInfo lineInfo, IXamlProperty property,
7272
}
7373
}
7474

75-
public XamlAstClrProperty(IXamlLineInfo lineInfo, string name, IXamlType declaringType,
76-
IXamlMethod? getter, IEnumerable<IXamlPropertySetter>? setters) : base(lineInfo)
75+
public XamlAstClrProperty(
76+
IXamlLineInfo lineInfo,
77+
string name,
78+
IXamlType declaringType,
79+
IXamlMethod? getter,
80+
IEnumerable<IXamlPropertySetter>? setters,
81+
IEnumerable<IXamlCustomAttribute>? customAttributes)
82+
: base(lineInfo)
7783
{
7884
Name = name;
7985
DeclaringType = declaringType;
@@ -83,13 +89,30 @@ public XamlAstClrProperty(IXamlLineInfo lineInfo, string name, IXamlType declari
8389
IsFamily = getter?.IsFamily == true;
8490
if (setters != null)
8591
Setters.AddRange(setters);
92+
if (customAttributes is not null)
93+
CustomAttributes.AddRange(customAttributes);
8694
}
8795

88-
public XamlAstClrProperty(IXamlLineInfo lineInfo, string name, IXamlType declaringType,
89-
IXamlMethod? getter, params IXamlMethod?[] setters) : this(lineInfo, name, declaringType,
90-
getter, setters.Where(x=> !(x is null)).Select(x => new XamlDirectCallPropertySetter(x!)))
96+
public XamlAstClrProperty(
97+
IXamlLineInfo lineInfo,
98+
string name,
99+
IXamlType declaringType,
100+
IXamlMethod? getter,
101+
IEnumerable<IXamlMethod?>? setters,
102+
IEnumerable<IXamlCustomAttribute>? customAttributes)
103+
: this(
104+
lineInfo,
105+
name,
106+
declaringType,
107+
getter,
108+
setters?.Where(x => x is not null).Select(x => new XamlDirectCallPropertySetter(x!)),
109+
customAttributes)
91110
{
111+
}
92112

113+
public XamlAstClrProperty(IXamlLineInfo lineInfo, string name, IXamlType declaringType, IXamlMethod? getter)
114+
: this(lineInfo, name, declaringType, getter, (IEnumerable<IXamlPropertySetter>?)null, null)
115+
{
93116
}
94117

95118
public override string ToString() => DeclaringType.GetFqn() + "." + Name;

src/XamlX/Transform/Transformers/PropertyReferenceResolver.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode nod
4040
&& p.Add != null);
4141
if (clrEvent != null)
4242
return new XamlAstClrProperty(prop,
43-
prop.Name, clrEvent.Add!.DeclaringType, null, clrEvent.Add);
43+
prop.Name, clrEvent.Add!.DeclaringType, null, [clrEvent.Add], null);
4444
}
4545

4646
// Look for attached properties on declaring type
@@ -68,16 +68,19 @@ public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode nod
6868
}
6969

7070
if (setter != null || getter != null)
71-
return new XamlAstClrProperty(prop, prop.Name, declaringType, getter, setter);
71+
{
72+
var customAttributes = setter?.GetParameterInfo(1).CustomAttributes;
73+
return new XamlAstClrProperty(prop, prop.Name, declaringType, getter, [setter], customAttributes);
74+
}
7275

7376
if (adder != null)
74-
return new XamlAstClrProperty(prop, prop.Name, declaringType, null, adder);
77+
return new XamlAstClrProperty(prop, prop.Name, declaringType, null, [adder], null);
7578

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

80-
XamlAstClrProperty FakeProperty() => new(prop, prop.Name, XamlPseudoType.Unknown, null, Array.Empty<IXamlMethod>());
83+
XamlAstClrProperty FakeProperty() => new(prop, prop.Name, XamlPseudoType.Unknown, null);
8184
}
8285

8386
return node;

src/XamlX/Transform/Transformers/ResolveContentPropertyTransformer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode nod
5656
adders.Select(a => new XamlDirectCallPropertySetter(a)
5757
{
5858
BinderParameters = {AllowMultiple = true}
59-
})),
59+
}), null),
6060
Array.Empty<IXamlAstValueNode>(),
6161
false);
6262
}

tests/XamlParserTests/DynamicSettersTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ public SpecialProperty(
495495
XamlAstClrProperty original,
496496
IXamlMethod getSpecialHandlerMethod,
497497
IXamlMethod handleMethod)
498-
: base(original, original.Name, original.DeclaringType, original.Getter, original.Setters)
498+
: base(original, original.Name, original.DeclaringType, original.Getter, original.Setters, null)
499499
=> Setters.Add(new SpecialHandlerPropertySetter(
500500
handleMethod.DeclaringType,
501501
handleMethod.Parameters[0],

0 commit comments

Comments
 (0)