From 27b6f625c800189ed82c750c8775a25f29c9ac07 Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Tue, 10 Jan 2023 17:47:01 +0000 Subject: [PATCH 1/5] Change Namespace to _Microsoft.Android.Resource.Designer --- .../Linker/MonoDroid.Tuner/FixLegacyResourceDesignerStep.cs | 2 +- .../Tasks/GenerateResourceDesignerIntermediateClass.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixLegacyResourceDesignerStep.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixLegacyResourceDesignerStep.cs index a08447e838f..d5ca2e4c65c 100644 --- a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixLegacyResourceDesignerStep.cs +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixLegacyResourceDesignerStep.cs @@ -20,7 +20,7 @@ namespace MonoDroid.Tuner public class FixLegacyResourceDesignerStep : LinkDesignerBase { internal const string DesignerAssemblyName = "_Microsoft.Android.Resource.Designer"; - internal const string DesignerAssemblyNamespace = "Microsoft.Android.Resource.Designer"; + internal const string DesignerAssemblyNamespace = "_Microsoft.Android.Resource.Designer"; bool designerLoaded = false; AssemblyDefinition designerAssembly = null; diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs index 9247993a554..4a720342701 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs @@ -19,7 +19,7 @@ public class GenerateResourceDesignerIntermediateClass : AndroidTask using System; namespace %NAMESPACE% { - public partial class Resource : %BASECLASS% { + public class Resource : %BASECLASS% { } } "; From ea1fa2e6dfcedb9ef94b07962a459ce18d13d95b Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Wed, 11 Jan 2023 09:59:09 +0000 Subject: [PATCH 2/5] Put partial back --- .../Tasks/GenerateResourceDesignerIntermediateClass.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs index 4a720342701..9247993a554 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs @@ -19,7 +19,7 @@ public class GenerateResourceDesignerIntermediateClass : AndroidTask using System; namespace %NAMESPACE% { - public class Resource : %BASECLASS% { + public partial class Resource : %BASECLASS% { } } "; From 9fa5836f2186ece5a86a433c97ce5ebee6384c28 Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Thu, 12 Jan 2023 15:30:12 +0000 Subject: [PATCH 3/5] Add a more '.net' looking auto genereted comment --- .../GenerateResourceDesignerIntermediateClass.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs index 9247993a554..131e34fc294 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs @@ -15,7 +15,11 @@ public class GenerateResourceDesignerIntermediateClass : AndroidTask private const string ResourceDesigner = $"{FixLegacyResourceDesignerStep.DesignerAssemblyNamespace}.Resource"; private const string ResourceDesignerConstants = $"{FixLegacyResourceDesignerStep.DesignerAssemblyNamespace}.ResourceConstant"; - private const string CSharpTemplate = @"// This is an Auto Generated file DO NOT EDIT + private const string CSharpTemplate = @"//------------------------------------------------------------------------------ +// +// This code was generated by a tool. DO NOT EDIT +// +//------------------------------------------------------------------------------ using System; namespace %NAMESPACE% { @@ -23,7 +27,11 @@ public partial class Resource : %BASECLASS% { } } "; - private const string FSharpTemplate = @"// This is an Auto Generated file DO NOT EDIT + private const string FSharpTemplate = @"//------------------------------------------------------------------------------ +// +// This code was generated by a tool. DO NOT EDIT +// +//------------------------------------------------------------------------------ namespace %NAMESPACE% type Resource = %BASECLASS% From 6df459f03feacc90d9d1039fdd126bde45649b0e Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Thu, 12 Jan 2023 16:26:33 +0000 Subject: [PATCH 4/5] Disable IDE0002 Simplify member access warning --- .../Tasks/GenerateResourceDesignerIntermediateClass.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs index 131e34fc294..b6d89a6aaf6 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerIntermediateClass.cs @@ -23,8 +23,10 @@ public class GenerateResourceDesignerIntermediateClass : AndroidTask using System; namespace %NAMESPACE% { + #pragma warning disable IDE0002 public partial class Resource : %BASECLASS% { } + #pragma warning restore IDE0002 } "; private const string FSharpTemplate = @"//------------------------------------------------------------------------------ From 8c4c56bcf72298b1f58cad65e4acb193971d980f Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Fri, 13 Jan 2023 15:19:04 +0000 Subject: [PATCH 5/5] Add System.ComponentModel.EditorBrowsableAttribute --- .../Tasks/GenerateResourceDesignerAssembly.cs | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerAssembly.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerAssembly.cs index ba009724f7b..3675c1a90bb 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerAssembly.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesignerAssembly.cs @@ -103,20 +103,25 @@ bool Run(DirectoryAssemblyResolver res) } else { // Add the InternalsVisibleToAttribute so the app can access ResourceConstant if (!string.IsNullOrEmpty (AssemblyName)) { - MethodReference internalsVisibleToAttributeConstructor = ImportCustomAttributeConstructor ("System.Runtime.CompilerServices.InternalsVisibleToAttribute", module, netstandardDef.MainModule); + MethodReference internalsVisibleToAttributeConstructor = ImportCustomAttributeConstructor ("System.Runtime.CompilerServices.InternalsVisibleToAttribute", module, netstandardDef.MainModule, argCount: 1); var ar = new CustomAttribute (internalsVisibleToAttributeConstructor); ar.ConstructorArguments.Add (new CustomAttributeArgument (module.TypeSystem.String, AssemblyName)); module.Assembly.CustomAttributes.Add (ar); } } - MethodReference targetFrameworkConstructor = ImportCustomAttributeConstructor ("System.Runtime.Versioning.TargetFrameworkAttribute", module, netstandardDef.MainModule); + MethodReference targetFrameworkConstructor = ImportCustomAttributeConstructor ("System.Runtime.Versioning.TargetFrameworkAttribute", module, netstandardDef.MainModule, argCount: 1); var attr = new CustomAttribute (targetFrameworkConstructor); attr.ConstructorArguments.Add (new CustomAttributeArgument (module.TypeSystem.String, $".NETStandard,Version=v2.1")); attr.Properties.Add (new CustomAttributeNamedArgument ("FrameworkDisplayName", new CustomAttributeArgument (module.TypeSystem.String, ""))); module.Assembly.CustomAttributes.Add (attr); + MethodReference editorBrowserConstructor = ImportCustomAttributeConstructor ("System.ComponentModel.EditorBrowsableAttribute", module, netstandardDef.MainModule, argCount: 1); + TypeReference e = ImportType ("System.ComponentModel.EditorBrowsableState", module, netstandardDef.MainModule); + var editorBrowserAttr = new CustomAttribute (editorBrowserConstructor); + editorBrowserAttr.ConstructorArguments.Add (new CustomAttributeArgument (e, System.ComponentModel.EditorBrowsableState.Never)); + var att = TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.Public | TypeAttributes.BeforeFieldInit; intArray = new ArrayType (module.TypeSystem.Int32); @@ -131,6 +136,7 @@ bool Run(DirectoryAssemblyResolver res) objectRef ); CreateCtor (resourceDesigner, module); + resourceDesigner.CustomAttributes.Add (editorBrowserAttr); module.Types.Add (resourceDesigner); TypeDefinition constDesigner = null; if (IsApplication) { @@ -143,6 +149,7 @@ bool Run(DirectoryAssemblyResolver res) objectRef ); CreateCtor (constDesigner, module); + constDesigner.CustomAttributes.Add (editorBrowserAttr); module.Types.Add (constDesigner); } @@ -196,11 +203,16 @@ bool Run(DirectoryAssemblyResolver res) return !Log.HasLoggedErrors; } - MethodReference ImportCustomAttributeConstructor (string type, ModuleDefinition module, ModuleDefinition sourceModule = null) + MethodReference ImportCustomAttributeConstructor (string type, ModuleDefinition module, ModuleDefinition sourceModule = null, int argCount = 0) { var tr = module.ImportReference ((sourceModule ?? module).ExportedTypes.First(x => x.FullName == type).Resolve ()); var tv = tr.Resolve(); - return module.ImportReference (tv.Methods.First(x => x.IsConstructor)); + return module.ImportReference (tv.Methods.First(x => x.IsConstructor && (x.Parameters?.Count ?? 0) == argCount)); + } + + TypeReference ImportType (string type, ModuleDefinition module, ModuleDefinition sourceModule = null) + { + return module.ImportReference ((sourceModule ?? module).ExportedTypes.First(x => x.FullName == type).Resolve ()); } void CreateIntProperty (string resourceClass, string propertyName, int value, TypeDefinition resourceDesigner, ModuleDefinition module,