Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions tests/assembly-preparer/ApplyPreserveAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -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 == "<Module>").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");
}
}
6 changes: 2 additions & 4 deletions tools/dotnet-linker/AppBundleRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1681,12 +1681,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));
Comment thread
rolfbjarne marked this conversation as resolved.
Resolved

if (signatures.Count == 0) {
// The type has no declared members, so add a placeholder member and preserve that,
Expand Down
16 changes: 16 additions & 0 deletions tools/dotnet-linker/DocumentionComments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 ('.', '#');
}
Comment thread
rolfbjarne marked this conversation as resolved.
Resolved

public static string GetSignature (MethodDefinition method)
{
var docCommentId = Mono.Cecil.Rocks.DocCommentId.GetDocCommentId (method);
Expand Down
Loading