Skip to content

Commit 5ffadb4

Browse files
[XSG] avoid generating XamlTypeResolver (#32169)
make sure all converters, extensions and vale providers requiring XamlTypeResolver are replaced at compile time, avoiding IL2026 and IL3050 warnings - fixes #32049
1 parent 0d9a596 commit 5ffadb4

File tree

5 files changed

+178
-41
lines changed

5 files changed

+178
-41
lines changed

src/Controls/tests/Xaml.UnitTests/CompiledTypeConverter.xaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
RectangleShape="Rectangle"
2222
RoundRectangleShape="RoundRectangle 1,2,3,4"
2323
PathShape="Path M16.484421,0.73799322C20.831404,0.7379931 24.353395,1.1259904 24.353395,1.6049905 24.353395,2.0839829 20.831404,2.4719803 16.484421,2.47198 12.138443,2.4719803 8.6154527,2.0839829 8.6154527,1.6049905 8.6154527,1.1259904 12.138443,0.7379931 16.484421,0.73799322z M1.9454784,0.061995983C2.7564723,5.2449602 12.246436,11.341911 12.246436,11.341911 13.248431,19.240842 9.6454477,17.915854 9.6454477,17.915854 7.9604563,18.897849 6.5314603,17.171859 6.5314603,17.171859 4.1084647,18.29585 3.279473,15.359877 3.2794733,15.359877 0.82348057,15.291876 1.2804796,11.362907 1.2804799,11.362907 -1.573514,10.239915 1.2344746,6.3909473 1.2344746,6.3909473 -1.3255138,4.9869594 1.9454782,0.061996057 1.9454784,0.061995983z M30.054371,0C30.054371,9.8700468E-08 33.325355,4.9249634 30.765367,6.3289513 30.765367,6.3289513 33.574364,10.177919 30.71837,11.30191 30.71837,11.30191 31.175369,15.22988 28.721384,15.297872 28.721384,15.297872 27.892376,18.232854 25.468389,17.110862 25.468389,17.110862 24.040392,18.835847 22.355402,17.853852 22.355402,17.853852 18.752417,19.178845 19.753414,11.279907 19.753414,11.279907 29.243385,5.1829566 30.054371,0z"
24-
List="Foo, Bar">
24+
List="Foo, Bar"
25+
ButtonType="Button"
26+
BindableProp="Label.Text">
27+
2528
<Grid ColumnDefinitions="*,Auto,1*,10" RowDefinitions="Auto,*,1*,10">
2629
<Grid>
2730
<Grid.ColumnDefinitions>

src/Controls/tests/Xaml.UnitTests/CompiledTypeConverter.xaml.cs

Lines changed: 71 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Microsoft.Maui.Controls.Xaml.UnitTests;
1414
public partial class CompiledTypeConverter : ContentPage
1515
{
1616
public static readonly BindableProperty RectangleBPProperty =
17-
BindableProperty.Create("RectangleBP", typeof(Rect), typeof(CompiledTypeConverter), default(Rect));
17+
BindableProperty.Create(nameof(RectangleBP), typeof(Rect), typeof(CompiledTypeConverter), default(Rect));
1818

1919
public Rect RectangleBP
2020
{
@@ -66,6 +66,11 @@ public Rect RectangleBP
6666
[System.ComponentModel.TypeConverter(typeof(ListStringTypeConverter))]
6767
public IList<string> List { get; set; }
6868

69+
[System.ComponentModel.TypeConverter(typeof(TypeTypeConverter))]
70+
public Type ButtonType { get; set; }
71+
72+
public BindableProperty BindableProp { get; set; }
73+
6974

7075
public CompiledTypeConverter() => InitializeComponent();
7176

@@ -104,12 +109,23 @@ public void CompiledTypeConverterAreInvoked([Values] XamlInflator xamlInflator)
104109
Assert.AreEqual(new Thickness(2, 3), p.label.Margin);
105110
Assert.AreEqual(2, p.List.Count);
106111
Assert.AreEqual("Bar", p.List[1]);
112+
Assert.AreEqual(typeof(Button), p.ButtonType);
113+
Assert.AreEqual(Label.TextProperty, p.BindableProp);
107114
}
108115

109116
[Test]
110117
public void ConvertersAreReplaced(
111118
[Values] XamlInflator inflator,
112-
[Values(typeof(BrushTypeConverter), typeof(ImageSourceConverter), typeof(StrokeShapeTypeConverter), typeof(Graphics.Converters.PointTypeConverter), typeof(Graphics.Converters.RectTypeConverter))] Type converterType)
119+
[Values(
120+
typeof(BrushTypeConverter),
121+
typeof(ImageSourceConverter),
122+
typeof(StrokeShapeTypeConverter),
123+
typeof(Graphics.Converters.PointTypeConverter),
124+
typeof(Graphics.Converters.RectTypeConverter),
125+
typeof(TypeTypeConverter),
126+
typeof(BindablePropertyConverter),
127+
typeof(ListStringTypeConverter)
128+
)] Type converterType)
113129
{
114130
if (inflator == XamlInflator.XamlC)
115131
{
@@ -134,62 +150,67 @@ public void ConvertersAreReplaced(
134150
135151
namespace Microsoft.Maui.Controls.Xaml.UnitTests;
136152
137-
[XamlProcessing(XamlInflator.Default, true)]
138-
public partial class CompiledTypeConverter : ContentPage
153+
[XamlProcessing(XamlInflator.Default, true)]
154+
public partial class CompiledTypeConverter : ContentPage
155+
{
156+
public static readonly BindableProperty RectangleBPProperty =
157+
BindableProperty.Create("RectangleBP", typeof(Rect), typeof(CompiledTypeConverter), default(Rect));
158+
159+
public Rect RectangleBP
139160
{
140-
public static readonly BindableProperty RectangleBPProperty =
141-
BindableProperty.Create("RectangleBP", typeof(Rect), typeof(CompiledTypeConverter), default(Rect));
161+
get { return (Rect)GetValue(RectangleBPProperty); }
162+
set { SetValue(RectangleBPProperty, value); }
163+
}
142164
143-
public Rect RectangleBP
144-
{
145-
get { return (Rect)GetValue(RectangleBPProperty); }
146-
set { SetValue(RectangleBPProperty, value); }
147-
}
165+
public Rect RectangleP { get; set; }
148166
149-
public Rect RectangleP { get; set; }
167+
public Point PointP { get; set; }
150168
151-
public Point PointP { get; set; }
169+
public Brush BrushByName { get; set; }
152170
153-
public Brush BrushByName { get; set; }
171+
public Brush BrushByARGB { get; set; }
154172
155-
public Brush BrushByARGB { get; set; }
173+
public Brush BrushByRGB { get; set; }
156174
157-
public Brush BrushByRGB { get; set; }
175+
public ImageSource ImageByUrl { get; set; }
158176
159-
public ImageSource ImageByUrl { get; set; }
177+
public ImageSource ImageByName { get; set; }
160178
161-
public ImageSource ImageByName { get; set; }
179+
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
180+
public IShape EllipseShape { get; set; }
162181
163-
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
164-
public IShape EllipseShape { get; set; }
182+
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
183+
public IShape LineShape { get; set; }
165184
166-
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
167-
public IShape LineShape { get; set; }
185+
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
186+
public IShape LineShapeTwo { get; set; }
168187
169-
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
170-
public IShape LineShapeTwo { get; set; }
188+
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
189+
public IShape LineShapeFour { get; set; }
171190
172-
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
173-
public IShape LineShapeFour { get; set; }
191+
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
192+
public IShape PolygonShape { get; set; }
174193
175-
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
176-
public IShape PolygonShape { get; set; }
194+
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
195+
public IShape PolylineShape { get; set; }
177196
178-
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
179-
public IShape PolylineShape { get; set; }
197+
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
198+
public IShape RectangleShape { get; set; }
180199
181-
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
182-
public IShape RectangleShape { get; set; }
200+
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
201+
public IShape RoundRectangleShape { get; set; }
183202
184-
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
185-
public IShape RoundRectangleShape { get; set; }
203+
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
204+
public IShape PathShape { get; set; }
186205
187-
[System.ComponentModel.TypeConverter(typeof(StrokeShapeTypeConverter))]
188-
public IShape PathShape { get; set; }
206+
[System.ComponentModel.TypeConverter(typeof(ListStringTypeConverter))]
207+
public IList<string> List { get; set; }
189208
190-
[System.ComponentModel.TypeConverter(typeof(ListStringTypeConverter))]
191-
public IList<string> List { get; set; }
209+
[System.ComponentModel.TypeConverter(typeof(TypeTypeConverter))]
210+
public Type ButtonType { get; set; }
192211
212+
public BindableProperty BindableProp { get; set; }
213+
}
193214
""")
194215
.RunMauiSourceGenerator(typeof(CompiledTypeConverter));
195216

@@ -198,9 +219,20 @@ public Rect RectangleBP
198219
var initComp = result.GeneratedInitializeComponent();
199220
if (converterType == typeof(Graphics.Converters.RectTypeConverter))
200221
Assert.True(result.GeneratedInitializeComponent().Contains("new global::Microsoft.Maui.Graphics.Rect(0, 1, 2, 4)", StringComparison.InvariantCulture));
201-
222+
if (converterType == typeof(Graphics.Converters.PointTypeConverter))
223+
Assert.True(result.GeneratedInitializeComponent().Contains("new global::Microsoft.Maui.Graphics.Point(1, 2)", StringComparison.InvariantCulture));
224+
if (converterType == typeof(TypeTypeConverter))
225+
Assert.True(result.GeneratedInitializeComponent().Contains("typeof(global::Microsoft.Maui.Controls.Button)", StringComparison.InvariantCulture));
226+
if (converterType == typeof(BindablePropertyConverter))
227+
Assert.True(result.GeneratedInitializeComponent().Contains("global::Microsoft.Maui.Controls.Label.TextProperty", StringComparison.InvariantCulture));
228+
if (converterType == typeof(ListStringTypeConverter))
229+
Assert.True(result.GeneratedInitializeComponent().Contains("new global::System.Collections.Generic.List<string> { \"Foo\", \"Bar\" }", StringComparison.InvariantCulture));
202230
// TODO check all other converters here
231+
203232
// TODO check that there are no ConvertFrom calls
233+
234+
//check that there are no serviceProvider
235+
Assert.False(result.GeneratedInitializeComponent().Contains("new global::Microsoft.Maui.Controls.Xaml.Internals.XamlServiceProvider(", StringComparison.InvariantCulture));
204236
}
205237
}
206238

src/Controls/tests/Xaml.UnitTests/DataTemplateExtension.xaml.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
using System;
2+
using System.Linq;
13
using NUnit.Framework;
24

5+
using static Microsoft.Maui.Controls.Xaml.UnitTests.MockSourceGenerator;
6+
37
namespace Microsoft.Maui.Controls.Xaml.UnitTests;
48

59
public partial class DataTemplateExtension : ContentPage
@@ -21,6 +25,29 @@ public void DataTemplateExtension([Values] XamlInflator inflator)
2125
var template = content.ContentTemplate;
2226
var obj = template.CreateContent();
2327
Assert.That(obj, Is.TypeOf<DataTemplateExtension>());
28+
}
29+
30+
public void ExtensionsAreReplaced([Values(XamlInflator.SourceGen)] XamlInflator inflator)
31+
{
32+
var result = CreateMauiCompilation()
33+
.WithAdditionalSource(
34+
"""
35+
using NUnit.Framework;
36+
37+
using static Microsoft.Maui.Controls.Xaml.UnitTests.MockSourceGenerator;
38+
39+
namespace Microsoft.Maui.Controls.Xaml.UnitTests;
40+
41+
public partial class DataTemplateExtension : ContentPage
42+
{
43+
public DataTemplateExtension() => InitializeComponent();
44+
}
45+
""")
46+
.RunMauiSourceGenerator(typeof(DataTemplateExtension));
47+
Assert.IsFalse(result.Diagnostics.Any());
48+
var initComp = result.GeneratedInitializeComponent();
49+
Assert.That(initComp.Contains("typeof(global::Microsoft.Maui.Controls.Xaml.UnitTests.DataTemplateExtension)", StringComparison.InvariantCulture));
50+
Assert.That(initComp, Does.Not.Contains("ProvideValue"));
2451
}
2552
}
2653
}

src/Controls/tests/Xaml.UnitTests/StyleTests.xaml.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
using System;
12
using System.Linq;
23
using Microsoft.Maui.Controls.Core.UnitTests;
34
using Microsoft.Maui.Graphics;
45
using NUnit.Framework;
56

7+
using static Microsoft.Maui.Controls.Xaml.UnitTests.MockSourceGenerator;
8+
69
namespace Microsoft.Maui.Controls.Xaml.UnitTests;
710

811
public partial class StyleTests : ContentPage
@@ -88,6 +91,30 @@ public void StylesDerivedFromDynamicStylesThroughDynamicResource([Values] XamlIn
8891

8992
Assert.AreEqual(50, label.FontSize);
9093
Assert.AreEqual(Colors.Red, label.TextColor);
94+
}
95+
96+
[Test]
97+
public void StyleCtorIsInvokedWithType([Values(XamlInflator.SourceGen)] XamlInflator inflator)
98+
{
99+
var result = CreateMauiCompilation()
100+
.WithAdditionalSource(
101+
"""
102+
using System.Linq;
103+
using Microsoft.Maui.Controls.Core.UnitTests;
104+
using Microsoft.Maui.Graphics;
105+
using NUnit.Framework;
106+
107+
namespace Microsoft.Maui.Controls.Xaml.UnitTests;
108+
109+
public partial class StyleTests : ContentPage
110+
{
111+
public StyleTests() => InitializeComponent();
112+
}
113+
""")
114+
.RunMauiSourceGenerator(typeof(StyleTests));
115+
Assert.IsFalse(result.Diagnostics.Any());
116+
var initComp = result.GeneratedInitializeComponent();
117+
Assert.That(initComp.Contains("new global::Microsoft.Maui.Controls.Style(typeof(global::Microsoft.Maui.Controls.Label))", StringComparison.InvariantCulture));
91118
}
92119
}
93120
}

src/Controls/tests/Xaml.UnitTests/TypeExtension.xaml.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System;
2+
using System.Linq;
23
using System.Windows.Input;
34
using Microsoft.Maui.Dispatching;
45
using Microsoft.Maui.UnitTests;
56
using NUnit.Framework;
67

7-
namespace Microsoft.Maui.Controls.Xaml.UnitTests;
8+
using static Microsoft.Maui.Controls.Xaml.UnitTests.MockSourceGenerator;
89

10+
namespace Microsoft.Maui.Controls.Xaml.UnitTests;
911
public enum NavigationOperation
1012
{
1113
Forward,
@@ -59,5 +61,51 @@ public void TypeExtensionSupportsNamespace([Values] XamlInflator inflator)
5961
var button = page.button0;
6062
Assert.That(button.CommandParameter, Is.EqualTo(typeof(TypeExtension)));
6163
}
64+
65+
[Test]
66+
public void ExtensionsAreReplaced([Values(XamlInflator.SourceGen)] XamlInflator inflator)
67+
{
68+
var result = CreateMauiCompilation()
69+
.WithAdditionalSource(
70+
"""
71+
using System;
72+
using System.Windows.Input;
73+
using Microsoft.Maui.Dispatching;
74+
using Microsoft.Maui.UnitTests;
75+
using NUnit.Framework;
76+
77+
using static Microsoft.Maui.Controls.Xaml.UnitTests.MockSourceGenerator;
78+
79+
namespace Microsoft.Maui.Controls.Xaml.UnitTests;
80+
public enum NavigationOperation
81+
{
82+
Forward,
83+
Back,
84+
Replace,
85+
}
86+
87+
[ContentProperty(nameof(Operation))]
88+
[AcceptEmptyServiceProvider]
89+
public class NavigateExtension : IMarkupExtension<ICommand>
90+
{
91+
public NavigationOperation Operation { get; set; }
92+
93+
public Type Type { get; set; }
94+
95+
public ICommand ProvideValue(IServiceProvider serviceProvider) => new Command(() => { });
96+
97+
object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider) => ProvideValue(serviceProvider);
98+
}
99+
100+
public partial class TypeExtension : ContentPage
101+
{
102+
public TypeExtension() => InitializeComponent();
103+
}
104+
""")
105+
.RunMauiSourceGenerator(typeof(TypeExtension));
106+
Assert.IsFalse(result.Diagnostics.Any());
107+
var initComp = result.GeneratedInitializeComponent();
108+
Assert.That(initComp.Contains("typeof(global::Microsoft.Maui.Controls.Xaml.UnitTests.TypeExtension)", StringComparison.InvariantCulture));
109+
}
62110
}
63111
}

0 commit comments

Comments
 (0)