Repository navigation
Commit 559b1f7
committed
chore: BindableType preserves properties and more
Context: unoplatform/uno.chefs#1712
Context: #21920
Context: unoplatform/uno.extensions#2966
Context: unoplatform/uno.extensions#2969
Context: https://discord.com/channels/732297728826277939/732297837953679412/1444909702512119899
While running the uno.chefs app on macOS under NativeAOT:
dotnet publish -c Release -r osx-x64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop -bl \
Chefs/Chefs.csproj \
-p:SelfContained=true -p:PublishAot=true -p:IsAotCompatible=true -p:UseSkiaRendering=true \
-p:IlcGenerateMapFile=true -p:IlcGenerateMstatFile=true -p:IlcGenerateDgmlFile=true \
-p:EmitCompilerGeneratedFiles=true -p:CompilerGeneratedFilesOutputPath=`pwd`/_gen
Chefs/bin/Release/net10.0-desktop/osx-x64/publish/Chefs
Console output would contain the following errors:
fail: Uno.UI.Dispatching.NativeDispatcher[0]
The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView]
fail: Uno.UI.Dispatching.NativeDispatcher[0]
The [Y] property getter does not exist on type [Windows.Foundation.Point]
fail: Uno.UI.Dispatching.NativeDispatcher[0]
The [X] property getter does not exist on type [Windows.Foundation.Point]
At this point, a description of the relation between NativeAOT and
Reflection would be useful.
*Parts* of System.Reflection can be used. Which parts *can* be used,
and how to use them, is oddly *not* well specified in the
[Native AOT deployment][0] documentation.
What doesn't work is outlined in the [limitations][1] section:
> * No dynamic loading, for example, `Assembly.LoadFile`.
> * No run-time code generation, for example, `System.Reflection.Emit`.
What *always* appears to work is:
* `Type.GetType()` returns a `Type` instance.
What ***can*** work is:
* `Type.GetType(string)`, *so long as* an assembly-qualified name
is used as a *string constant*
// bad
string name = GetSomeTypeName();
Type type = Type.GetType(name);
// good
Type type = Type.GetType("My.Example.TypeName, ExampleAssembly");
* `Type.GetMethod(string)`, but see fine print below.
* `Type.GetProperty(string)`, but see fine print below.
* `MethodInfo.Invoke()`
* …
**The Fine Print**: in order for many Reflection-based APIs such as
`Type.GetMethod(string)` to work, the program must contain
"reflection metadata."
We can now describe, briefly, what NativeAOT does:
1. Accept IL/assemblies as input
2. Internally trim the IL, optimizes them, etc.
3. Generates a native binary containing two separate bits of data:
* Native machine code for execution
* "reflection metadata"
You can get an inkling of what methods survived trimming and become
native code by using `dotnet publish -p:IlcGenerateMstatFile=true …`,
which will produce a `.mstat` file, a PE file which references every
type and member which is in the native binary, ***after*** inlining.
(Meaning if a member is inlined, it *won't* be present!)
Use e.g. `monodis --memberref App.mstat` to list post-trimmed members.
You can get an inkling of the "reflection metadata" that is present
by using `dotnet publish -p:IlcGenerateMetadataLog=true …`, which
produces a `.metadata.csv` file. It's not particularly scrutible,
but can be used to verify observed behavior. Consider one of the above
failure messages:
The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView]
An obvious question to ask: is the `TabNavigation` property in the
reflection metadata? Search for `"TabNavigation"` (with quotes!),
and it's *not* in there. Compare to when this commit is in use, and:
66087019, Property, "TabNavigation", "3410be9a 6810bea8 5410beae 5410beb2"
3410be9a, ConstantStringValue, "TabNavigation", ""
The 3rd column is the property name `TabNavigation`, while the 2nd
column is the type; for properties, we want `Property`. The 4th
column is "Children", one of which is:
5410beae, MethodSemantics, "Getter : [HasThis] Microsoft.UI.Xaml.Input.KeyboardNavigationMode get_TabNavigation()", "50086c65"
which at least verifies that `TabNavigation` is in play.
Programs can control what is contained within reflection metadata
by using string constants with Reflection APIs, for example:
typeof(SomeKnownType).GetMethod("ConstantName")
will ensure that reflection metadata contains
`SomeKnownType.ConstantName()`, and `ConstantName()` will be returned
from `typeof(SomeKnownType).GetMethods()` and can be invoked with
`MethodInfo.Invoke()`.
If you *don't* use `typeof(SomeKnownType)` or string constants with
`Type.GetType(string)` or `type.GetMethod("constant")`, then you need
to "suggest" that some pieces of information be added to
reflection metadata.
The two predominant ways to do this are via the custom attributes:
* [`DynamicallyAccessedMembersAttribute`][2]
* [`DynamicDependencyAttribute`][3]
From the Uno perspective, enter `BindingPropertyHelper`: *everything*
is based on Reflection!
Fortunately NativeAOT "supports" Reflection! (See above.)
*Unfortunately*, we now need to convince NativeAOT to store the
reflection metadata Uno requires in order to work!
Various other PRs have dealt with some of this, such as #21920.
Returning to the original failure messages, e.g.
fail: Uno.UI.Dispatching.NativeDispatcher[0]
The [TabNavigation] property getter does not exist on type [Microsoft.UI.Xaml.Controls.FlipView]
During the app build, `src/SourceGenerators/Uno.UI.SourceGenerators`
generates a `BindableMetadata.g.cs` file which will mention
`[Bindable]` types, e.g. for `Microsoft.UI.Xaml.Controls.FlipView`:
/// <summary>
/// Builder for Microsoft.UI.Xaml.Controls.FlipView
/// </summary>
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute]
static class MetadataBuilder_239
{
internal static global::Uno.UI.DataBinding.IBindableType Build(global::Uno.UI.DataBinding.BindableType parent)
{
var bindableType = parent ?? new global::Uno.UI.DataBinding.BindableType(151, typeof(global::Microsoft.UI.Xaml.Controls.FlipView));
// …
}
private static object CreateInstance() => new global::Microsoft.UI.Xaml.Controls.FlipView();
}
`BindableMetadata.g.cs` was needed in the ye olde Xamarin world order
to tell *its* linker to e.g. "preserve the `FlipView` default ctor!"
*Now*, we can build upon this infrastructure and update `BindableType`
to request that Reflection information regarding properties, fields,
and constructors be retained. We can do this by updating the
`BindableType` constructor:
partial class BindableType {
internal const DynamicallyAccessedMemberTypes TypeRequirements =
DynamicallyAccessedMemberTypes.PublicProperties | …;
public BindableType(int estimatedPropertySize, [DynamicallyAccessedMembers(TypeRequirements)] Type sourceType) => …
}
*Because* we have `BindableMetadata.g.cs` *and* because it provides
`typeof(FlipView)`, this directs NativeAOT to make property and other
reflection metadata available to the app, *fixing* the error messages.
Expand this addition of `[DynamicallyAccessedMembers]` to also include
`BindableProperty` and related types.
Note: the IL2111 warning "Method 'System.Type.TypeInitializer.get' …"
is *caused by* `typeof(Type)` (?!), which is particularly odd given
that we don't use `Type.TypeInitializer`!
Thanks to Alexander Köplinger for [looking into this][4].
[0]: https://learn.microsoft.comdotnet/core/deploying/native-aot
[1]: https://learn.microsoft.comdotnet/core/deploying/native-aot/#limitations-of-native-aot-deployment
[2]: https://learn.microsoft.comdotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembersattribute?view=net-10.0
[3]: https://learn.microsoft.comdotnet/api/system.diagnostics.codeanalysis.dynamicdependencyattribute?view=net-10.0
[4]: https://discord.com/channels/732297728826277939/732297837953679412/14457736771521741501 parent c3a5b3f commit 559b1f7
8 files changed
Lines changed: 60 additions & 12 deletions
File tree
- src
- SourceGenerators/Uno.UI.SourceGenerators/BindableTypeProviders
- Uno.UI
- DataBinding
- UI/Xaml
- Controls/Frame
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
302 | 302 | | |
303 | 303 | | |
304 | 304 | | |
| 305 | + | |
305 | 306 | | |
306 | 307 | | |
307 | 308 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
27 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
28 | 33 | | |
29 | 34 | | |
30 | 35 | | |
| |||
35 | 40 | | |
36 | 41 | | |
37 | 42 | | |
| 43 | + | |
38 | 44 | | |
39 | 45 | | |
40 | 46 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
19 | 27 | | |
20 | 28 | | |
21 | 29 | | |
| |||
26 | 34 | | |
27 | 35 | | |
28 | 36 | | |
29 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
30 | 41 | | |
31 | 42 | | |
32 | 43 | | |
33 | 44 | | |
34 | 45 | | |
| 46 | + | |
35 | 47 | | |
36 | 48 | | |
37 | 49 | | |
| |||
61 | 73 | | |
62 | 74 | | |
63 | 75 | | |
64 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
65 | 79 | | |
66 | 80 | | |
67 | 81 | | |
| |||
71 | 85 | | |
72 | 86 | | |
73 | 87 | | |
74 | | - | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
75 | 94 | | |
76 | 95 | | |
77 | 96 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| 30 | + | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| 35 | + | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
117 | 118 | | |
118 | 119 | | |
119 | 120 | | |
| 121 | + | |
| 122 | + | |
120 | 123 | | |
121 | 124 | | |
122 | 125 | | |
| |||
174 | 177 | | |
175 | 178 | | |
176 | 179 | | |
| 180 | + | |
| 181 | + | |
177 | 182 | | |
178 | 183 | | |
179 | 184 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| 58 | + | |
57 | 59 | | |
| 60 | + | |
58 | 61 | | |
59 | 62 | | |
60 | 63 | | |
61 | 64 | | |
62 | 65 | | |
63 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
64 | 74 | | |
65 | 75 | | |
66 | 76 | | |
| |||
140 | 150 | | |
141 | 151 | | |
142 | 152 | | |
143 | | - | |
144 | | - | |
| 153 | + | |
| 154 | + | |
145 | 155 | | |
146 | 156 | | |
147 | 157 | | |
| |||
196 | 206 | | |
197 | 207 | | |
198 | 208 | | |
199 | | - | |
200 | | - | |
| 209 | + | |
| 210 | + | |
201 | 211 | | |
202 | 212 | | |
203 | 213 | | |
| |||
214 | 224 | | |
215 | 225 | | |
216 | 226 | | |
217 | | - | |
218 | | - | |
| 227 | + | |
| 228 | + | |
219 | 229 | | |
220 | 230 | | |
221 | 231 | | |
| |||
297 | 307 | | |
298 | 308 | | |
299 | 309 | | |
| 310 | + | |
300 | 311 | | |
301 | 312 | | |
302 | 313 | | |
303 | 314 | | |
304 | 315 | | |
| 316 | + | |
305 | 317 | | |
306 | 318 | | |
307 | 319 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
| 41 | + | |
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
| |||
0 commit comments