diff --git a/tests/assembly-preparer/ApplyPreserveAttributeTests.cs b/tests/assembly-preparer/ApplyPreserveAttributeTests.cs new file mode 100644 index 000000000000..252e0e64c638 --- /dev/null +++ b/tests/assembly-preparer/ApplyPreserveAttributeTests.cs @@ -0,0 +1,51 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using Mono.Cecil.Rocks; + +namespace AssemblyPreparerTests; + +public class ApplyPreserveAttributeTests : BaseClass { + [Test] + [TestCase (ApplePlatform.MacCatalyst, false)] + [TestCase (ApplePlatform.iOS, false)] + [TestCase (ApplePlatform.TVOS, false)] + [TestCase (ApplePlatform.MacOSX, true)] + public void ExplicitInterfaceMembers (ApplePlatform platform, bool isCoreCLR) + { + var code = @" + using System; + using Foundation; + + interface IMyInterface { + int MyProperty { get; set; } + event EventHandler MyEvent; + } + + [Preserve (AllMembers = true)] + class MyClass : IMyInterface { + int IMyInterface.MyProperty { get; set; } + event EventHandler IMyInterface.MyEvent { + add {} + remove {} + } + } + "; + + AssertPrepare (platform, isCoreCLR, code, out var assemblyDefinition); + + var moduleConstructor = assemblyDefinition.MainModule.Types.Single (v => v.Name == "").GetStaticConstructor (); + Assert.That (moduleConstructor, Is.Not.Null, "Module constructor"); + var signatures = moduleConstructor.CustomAttributes + .Where (v => v.AttributeType.Name == "DynamicDependencyAttribute") + .Where (v => v.ConstructorArguments.Count == 2) + .Where (v => v.ConstructorArguments [0].Value is string) + .Where (v => ((TypeReference) v.ConstructorArguments [1].Value).FullName == "MyClass") + .Select (v => (string) v.ConstructorArguments [0].Value) + .ToArray (); + + Assert.That (signatures, Does.Contain ("IMyInterface#MyProperty"), "Property"); + Assert.That (signatures, Does.Contain ("IMyInterface#MyEvent"), "Event"); + Assert.That (signatures, Has.None.Contains ("IMyInterface."), "Unescaped explicit interface member"); + } +} diff --git a/tools/dotnet-linker/AppBundleRewriter.cs b/tools/dotnet-linker/AppBundleRewriter.cs index 6e478c732ab5..1f0c7dde22d0 100644 --- a/tools/dotnet-linker/AppBundleRewriter.cs +++ b/tools/dotnet-linker/AppBundleRewriter.cs @@ -1678,12 +1678,10 @@ public bool AddPreserveAllMembersDynamicDependencyAttributes (MethodDefinition a if (!method.HasCustomAttribute ("System.Runtime.CompilerServices", "CompilerGeneratedAttribute")) signatures.Add (DocumentationComments.GetNameSignature (method)); } - // Properties and events don't have a documentation comment signature helper, but the trimmer will - // match any member with the given name, which is good enough (and it's what we want here anyway). foreach (var property in type.Properties) - signatures.Add (property.Name); + signatures.Add (DocumentationComments.GetSignature (property)); foreach (var @event in type.Events) - signatures.Add (@event.Name); + signatures.Add (DocumentationComments.GetSignature (@event)); if (signatures.Count == 0) { // The type has no declared members, so add a placeholder member and preserve that, diff --git a/tools/dotnet-linker/DocumentionComments.cs b/tools/dotnet-linker/DocumentionComments.cs index 0a3373d7fae8..0e8fa2e8b86a 100644 --- a/tools/dotnet-linker/DocumentionComments.cs +++ b/tools/dotnet-linker/DocumentionComments.cs @@ -19,6 +19,12 @@ public static string GetSignature (IMetadataTokenProvider member) if (member is MethodDefinition md) return GetSignature (md); + if (member is PropertyDefinition pd) + return GetSignature (pd); + + if (member is EventDefinition ed) + return GetSignature (ed); + if (member is TypeDefinition td) return GetSignature (td); @@ -37,6 +43,16 @@ public static string GetSignature (FieldDefinition field) return field.Name.Replace ('.', '#'); } + public static string GetSignature (PropertyDefinition property) + { + return property.Name.Replace ('.', '#'); + } + + public static string GetSignature (EventDefinition @event) + { + return @event.Name.Replace ('.', '#'); + } + public static string GetSignature (MethodDefinition method) { var docCommentId = Mono.Cecil.Rocks.DocCommentId.GetDocCommentId (method);