Skip to content

chore: enable logging on -desktop Platforms - #1712

Merged
kazo0 merged 1 commit into
mainfrom
dev/jonpryor/jonp-log-messages-on-desktop
Dec 2, 2025
Merged

kazo0 merged 1 commit into
mainfrom
dev/jonpryor/jonp-log-messages-on-desktop

Conversation

@jonpryor

@jonpryor jonpryor commented Dec 2, 2025

Copy link
Copy Markdown
Collaborator

Context: #1709

It is extremely helpful to have log messages written to stdout when running on -desktop platforms such as macOS.

Update Desktop/Program.cs to call App.InitializeLogging() so that messages are written to stdout.

GitHub Issue (If applicable): #

PR Type

What kind of change does this PR introduce?

What is the current behavior?

What is the new behavior?

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tested code with current supported SDKs
  • Docs have been added/updated which fit documentation template. (for bug fixes / features)
  • Unit Tests and/or UI Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Wasm UI Tests are not showing unexpected any differences. Validate PR Screenshots Compare Test Run results.
  • Contains NO breaking changes
  • Updated the Release Notes
  • Associated with an issue (GitHub or internal)

Other information

Internal Issue (If applicable):

Context: #1709

It is extremely helpful to have log messages written to stdout
when running on -desktop platforms such as macOS.

Update `Desktop/Program.cs` to call `App.InitializeLogging()` so that
messages are written to stdout.
@kazo0
kazo0 enabled auto-merge (squash) December 2, 2025 17:46
@kazo0
kazo0 merged commit 16a62fd into main Dec 2, 2025
@kazo0
kazo0 deleted the dev/jonpryor/jonp-log-messages-on-desktop branch December 2, 2025 18:01
jonpryor added a commit to unoplatform/uno that referenced this pull request Dec 2, 2025
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

(along with a change from unoplatform/uno.chefs#1712)

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.GetMethods()`, but see fine print below.
  * `Type.GetProperties()`, but see fine print below.
  * `MethodInfo.Invoke()`
  * …

**The Fine Print**: in order for many Reflection-based APIs such as
`Type.GetMethods()` to work, the program must contain
"reflection metadata."

We can now describe, briefly, what NativeAOT does:

 1. Accept IL/assemblies
 2. Internally trims the IL, optimizes them, etc.
 3. Generates a native binary containing two separate bits of data:

    * Native machine code for execution
    * "reflection metadata"

There is no facility to separately dump reflection metadata.

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!

*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 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.

[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
jonpryor added a commit to unoplatform/uno that referenced this pull request Dec 3, 2025
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

(along with a change from unoplatform/uno.chefs#1712)

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.GetMethods()`, but see fine print below.
  * `Type.GetProperties()`, but see fine print below.
  * `MethodInfo.Invoke()`
  * …

**The Fine Print**: in order for many Reflection-based APIs such as
`Type.GetMethods()` to work, the program must contain
"reflection metadata."

We can now describe, briefly, what NativeAOT does:

 1. Accept IL/assemblies
 2. Internally trims the IL, optimizes them, etc.
 3. Generates a native binary containing two separate bits of data:

    * Native machine code for execution
    * "reflection metadata"

There is no facility to separately dump reflection metadata.

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!

*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 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.

[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
jonpryor added a commit to unoplatform/uno that referenced this pull request Dec 3, 2025
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

(along with a change from unoplatform/uno.chefs#1712)

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.GetMethods()`, but see fine print below.
  * `Type.GetProperties()`, but see fine print below.
  * `MethodInfo.Invoke()`
  * …

**The Fine Print**: in order for many Reflection-based APIs such as
`Type.GetMethods()` to work, the program must contain
"reflection metadata."

We can now describe, briefly, what NativeAOT does:

 1. Accept IL/assemblies
 2. Internally trims the IL, optimizes them, etc.
 3. Generates a native binary containing two separate bits of data:

    * Native machine code for execution
    * "reflection metadata"

There is no facility to separately dump reflection metadata.

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!

*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 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.

[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
jonpryor added a commit to unoplatform/uno that referenced this pull request Dec 3, 2025
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:IlcGenerateMapFile=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/1445773677152174150
jonpryor added a commit to unoplatform/uno that referenced this pull request Dec 3, 2025
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:IlcGenerateMapFile=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/1445773677152174150
jonpryor added a commit to unoplatform/uno that referenced this pull request Dec 4, 2025
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/1445773677152174150
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants