From 83db587db96e02b3a546bd04ad94c54050af36ec Mon Sep 17 00:00:00 2001 From: Eli Arbel Date: Sun, 28 Feb 2021 13:35:51 +0200 Subject: [PATCH] Support non-empty method bodies (fixes #6) --- README.md | 8 ++++++++ src/Build/IgnoresAccessChecksToGenerator.targets | 2 +- .../IgnoresAccessChecksToGenerator.Tasks.csproj | 1 + .../IgnoresAccessChecksToGenerator.nuspec | 14 +++++++------- .../PublicizeInternals.cs | 11 +++++++++-- .../IgnoresAccessChecksToGenerator.Test.csproj | 5 +++-- 6 files changed, 29 insertions(+), 12 deletions(-) rename src/{ => IgnoresAccessChecksToGenerator.Tasks}/IgnoresAccessChecksToGenerator.nuspec (57%) diff --git a/README.md b/README.md index c434bb0..0e23392 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,11 @@ Just add the package and define the `InternalsAssemblyNames` property with a sem ``` + +By default, the build tasks replaces all method bodies with `throw null;`. To keep the original bodies, you can specify: + +```xml + + false + +``` diff --git a/src/Build/IgnoresAccessChecksToGenerator.targets b/src/Build/IgnoresAccessChecksToGenerator.targets index 40e591f..6a6aee8 100644 --- a/src/Build/IgnoresAccessChecksToGenerator.targets +++ b/src/Build/IgnoresAccessChecksToGenerator.targets @@ -9,7 +9,7 @@ - + diff --git a/src/IgnoresAccessChecksToGenerator.Tasks/IgnoresAccessChecksToGenerator.Tasks.csproj b/src/IgnoresAccessChecksToGenerator.Tasks/IgnoresAccessChecksToGenerator.Tasks.csproj index 32e033c..e8219b7 100644 --- a/src/IgnoresAccessChecksToGenerator.Tasks/IgnoresAccessChecksToGenerator.Tasks.csproj +++ b/src/IgnoresAccessChecksToGenerator.Tasks/IgnoresAccessChecksToGenerator.Tasks.csproj @@ -3,6 +3,7 @@ netstandard2.0;net46 PackageReference + IgnoresAccessChecksToGenerator.nuspec diff --git a/src/IgnoresAccessChecksToGenerator.nuspec b/src/IgnoresAccessChecksToGenerator.Tasks/IgnoresAccessChecksToGenerator.nuspec similarity index 57% rename from src/IgnoresAccessChecksToGenerator.nuspec rename to src/IgnoresAccessChecksToGenerator.Tasks/IgnoresAccessChecksToGenerator.nuspec index 568307b..c5f88a4 100644 --- a/src/IgnoresAccessChecksToGenerator.nuspec +++ b/src/IgnoresAccessChecksToGenerator.Tasks/IgnoresAccessChecksToGenerator.nuspec @@ -1,21 +1,21 @@ - + IgnoresAccessChecksToGenerator - 0.4.0 + 0.5.0 aelij aelij - https://github.com/aelij/IgnoresAccessChecksToGenerator/blob/master/LICENSE + MIT https://github.com/aelij/IgnoresAccessChecksToGenerator false Generates IgnoresAccessChecksTo attributes and reference assemblies to allow compile-time access to internals - Copyright 2017 + Copyright 2021 IgnoresAccessChecksToGenerator internals - - - + + + \ No newline at end of file diff --git a/src/IgnoresAccessChecksToGenerator.Tasks/PublicizeInternals.cs b/src/IgnoresAccessChecksToGenerator.Tasks/PublicizeInternals.cs index 3fc067c..b95a477 100644 --- a/src/IgnoresAccessChecksToGenerator.Tasks/PublicizeInternals.cs +++ b/src/IgnoresAccessChecksToGenerator.Tasks/PublicizeInternals.cs @@ -24,6 +24,8 @@ public class PublicizeInternals : Task public string ExcludeTypeNames { get; set; } + public bool UseEmptyMethodBodies { get; set; } = true; + [Output] public ITaskItem[] TargetReferences { get; set; } @@ -150,8 +152,13 @@ private void CreatePublicAssembly(string source, string target) foreach (var method in type.Methods) { - method.Body?.Instructions?.Clear(); - method.Body?.ExceptionHandlers?.Clear(); + if (UseEmptyMethodBodies && method.HasBody) + { + var emptyBody = new Mono.Cecil.Cil.MethodBody(method); + emptyBody.Instructions.Add(Mono.Cecil.Cil.Instruction.Create(Mono.Cecil.Cil.OpCodes.Ldnull)); + emptyBody.Instructions.Add(Mono.Cecil.Cil.Instruction.Create(Mono.Cecil.Cil.OpCodes.Throw)); + method.Body = emptyBody; + } if (method.IsAssembly || method.IsFamilyOrAssembly || diff --git a/test/IgnoresAccessChecksToGenerator.Test/IgnoresAccessChecksToGenerator.Test.csproj b/test/IgnoresAccessChecksToGenerator.Test/IgnoresAccessChecksToGenerator.Test.csproj index 9aea038..a0663e3 100644 --- a/test/IgnoresAccessChecksToGenerator.Test/IgnoresAccessChecksToGenerator.Test.csproj +++ b/test/IgnoresAccessChecksToGenerator.Test/IgnoresAccessChecksToGenerator.Test.csproj @@ -1,13 +1,14 @@  - netcoreapp1.1 + netcoreapp3.1 Exe Microsoft.CodeAnalysis;Microsoft.CodeAnalysis.CSharp + false - +