From e8592507c66f8e06f59ba64b1a0355bc4dbca069 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Mon, 28 Aug 2023 22:45:43 +0200 Subject: [PATCH 1/2] Fix DynamicDependency attributes for generic types and methods --- tools/dotnet-linker/AppBundleRewriter.cs | 28 ++++++++++++++++++++++ tools/dotnet-linker/DocumentionComments.cs | 18 +++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/tools/dotnet-linker/AppBundleRewriter.cs b/tools/dotnet-linker/AppBundleRewriter.cs index bbb7a4cacafe..7dfbeb340863 100644 --- a/tools/dotnet-linker/AppBundleRewriter.cs +++ b/tools/dotnet-linker/AppBundleRewriter.cs @@ -498,6 +498,19 @@ public FieldReference GetFieldReference (AssemblyDefinition assembly, TypeRefere } } + public MethodReference DynamicDependencyAttribute_ctor__String_String_String { + get { + return GetMethodReference (CorlibAssembly, + System_Diagnostics_CodeAnalysis_DynamicDependencyAttribute, + ".ctor", + ".ctor(String,String,String)", + isStatic: false, + System_String, + System_String, + System_String); + } + } + public MethodReference RuntimeTypeHandle_Equals { get { if (configuration.Application.XamarinRuntime == XamarinRuntime.MonoVM) { @@ -1246,12 +1259,27 @@ public void ClearCurrentAssembly () public CustomAttribute CreateDynamicDependencyAttribute (string memberSignature, TypeDefinition type) { + if (type.HasGenericParameters) { + var typeName = Xamarin.Utils.DocumentationComments.GetSignature (type); + var assemblyName = type.Module.Assembly.Name.Name; + return CreateDynamicDependencyAttribute (memberSignature, typeName, assemblyName); + } + var attribute = new CustomAttribute (DynamicDependencyAttribute_ctor__String_Type); attribute.ConstructorArguments.Add (new CustomAttributeArgument (System_String, memberSignature)); attribute.ConstructorArguments.Add (new CustomAttributeArgument (System_Type, type)); return attribute; } + public CustomAttribute CreateDynamicDependencyAttribute (string memberSignature, string typeName, string assemblyName) + { + var attribute = new CustomAttribute (DynamicDependencyAttribute_ctor__String_String_String); + attribute.ConstructorArguments.Add (new CustomAttributeArgument (System_String, memberSignature)); + attribute.ConstructorArguments.Add (new CustomAttributeArgument (System_String, typeName)); + attribute.ConstructorArguments.Add (new CustomAttributeArgument (System_String, assemblyName)); + return attribute; + } + public CustomAttribute CreateDynamicDependencyAttribute (DynamicallyAccessedMemberTypes memberTypes, TypeDefinition type) { var attribute = new CustomAttribute (DynamicDependencyAttribute_ctor__DynamicallyAccessedMemberTypes_Type); diff --git a/tools/dotnet-linker/DocumentionComments.cs b/tools/dotnet-linker/DocumentionComments.cs index 49f855800caf..cdcbab38122f 100644 --- a/tools/dotnet-linker/DocumentionComments.cs +++ b/tools/dotnet-linker/DocumentionComments.cs @@ -28,7 +28,7 @@ public static string GetSignature (IMetadataTokenProvider member) public static string GetSignature (TypeDefinition type) { if (type.IsNested) - return type.Name; + return type.FullName.Replace ('/', '.'); return type.FullName; } @@ -41,6 +41,11 @@ public static string GetSignature (MethodDefinition method) { var sb = new StringBuilder (); sb.Append (method.Name.Replace ('.', '#')); + + if (method.HasGenericParameters) { + sb.Append ($"``{method.GenericParameters.Count}"); + } + sb.Append ('('); for (var i = 0; i < method.Parameters.Count; i++) { if (i > 0) @@ -74,6 +79,17 @@ static void WriteTypeSignature (StringBuilder sb, TypeReference type) return; } + if (type is GenericParameter gp) { + if (gp.Type == GenericParameterType.Type) { + sb.Append('`'); + } else { + sb.Append("``"); + } + + sb.Append(gp.Position.ToString()); + return; + } + sb.Append (type.FullName.Replace ('/', '.')); } } From 46365af7174a45b957ea08751fe01b557db6d9ea Mon Sep 17 00:00:00 2001 From: GitHub Actions Autoformatter Date: Mon, 28 Aug 2023 21:11:32 +0000 Subject: [PATCH 2/2] Auto-format source code --- tools/dotnet-linker/DocumentionComments.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/dotnet-linker/DocumentionComments.cs b/tools/dotnet-linker/DocumentionComments.cs index cdcbab38122f..7ac63b21a0aa 100644 --- a/tools/dotnet-linker/DocumentionComments.cs +++ b/tools/dotnet-linker/DocumentionComments.cs @@ -81,12 +81,12 @@ static void WriteTypeSignature (StringBuilder sb, TypeReference type) if (type is GenericParameter gp) { if (gp.Type == GenericParameterType.Type) { - sb.Append('`'); + sb.Append ('`'); } else { - sb.Append("``"); + sb.Append ("``"); } - sb.Append(gp.Position.ToString()); + sb.Append (gp.Position.ToString ()); return; }