From 0e318b951c83cbf410f1140b8c15bd63e331bdc9 Mon Sep 17 00:00:00 2001 From: Chris Pulman Date: Fri, 26 Apr 2024 21:01:33 +0100 Subject: [PATCH] Fix for WireUpControls throws exception (#3795) **What kind of change does this PR introduce?** Fix for #3714 **What is the current behavior?** WireUpControls throws exception with .Net 8.0 Android **What is the new behavior?** WireUpControls works with .Net 8.0 Android **What might this PR break?** none expected **Please check if the PR fulfills these requirements** - [ ] Tests for the changes have been added (for bug fixes / features) - [ ] Docs have been added / updated (for bug fixes / features) **Other information**: --- .../Platforms/android/ControlFetcherMixin.cs | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/ReactiveUI/Platforms/android/ControlFetcherMixin.cs b/src/ReactiveUI/Platforms/android/ControlFetcherMixin.cs index 1704d7400e..cf08e4ccf4 100644 --- a/src/ReactiveUI/Platforms/android/ControlFetcherMixin.cs +++ b/src/ReactiveUI/Platforms/android/ControlFetcherMixin.cs @@ -227,16 +227,19 @@ private static int GetControlIdByName(Assembly assembly, string? name) assembly, currentAssembly => { - var resources = currentAssembly.GetModules().SelectMany(x => x.GetTypes()).First(x => x.Name == "Resource"); - - var idType = resources.GetNestedType("Id"); - - if (idType is null) - { - throw new InvalidOperationException("Id is not a valid nested type in the resources."); - } - - return idType.GetFields() +#if NET8_0_OR_GREATER + var resources = Assembly.Load(currentAssembly + .GetReferencedAssemblies() + .First(an => an.FullName.StartsWith("_Microsoft.Android.Resource.Designer")).ToString()) + .GetModules() + .SelectMany(x => x.GetTypes()) + .First(x => x.Name == "ResourceConstant"); +#else + var resources = currentAssembly.GetModules().SelectMany(x => x.GetTypes()).First(x => x.Name == "Resource"); +#endif + + var idType = resources.GetNestedType("Id") ?? throw new InvalidOperationException("Id is not a valid nested type in the resources."); + return idType.GetFields() .Where(x => x.FieldType == typeof(int)) .ToDictionary(k => k.Name, v => ((int?)v.GetRawConstantValue()) ?? 0, StringComparer.InvariantCultureIgnoreCase); });