Skip to content

Commit

Permalink
Support non-empty method bodies (fixes #6)
Browse files Browse the repository at this point in the history
  • Loading branch information
aelij committed Feb 28, 2021
1 parent 6b3955d commit 83db587
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ Just add the package and define the `InternalsAssemblyNames` property with a sem

</Project>
```

By default, the build tasks replaces all method bodies with `throw null;`. To keep the original bodies, you can specify:

```xml
<PropertyGroup>
<InternalsAssemblyUseEmptyMethodBodies>false</InternalsAssemblyUseEmptyMethodBodies>
</PropertyGroup>
```
2 changes: 1 addition & 1 deletion src/Build/IgnoresAccessChecksToGenerator.targets
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<UsingTask AssemblyFile="$(_IACTG_TaskAssembly)" TaskName="IgnoresAccessChecksToGenerator.Tasks.PublicizeInternals" />

<Target Name="IgnoresAccessChecksToGenerator" AfterTargets="AfterResolveReferences">
<PublicizeInternals SourceReferences="@(ReferencePath)" AssemblyNames="$(InternalsAssemblyNames)" ExcludeTypeNames="$(InternalsAssemblyExcludeTypeNames)">
<PublicizeInternals SourceReferences="@(ReferencePath)" AssemblyNames="$(InternalsAssemblyNames)" ExcludeTypeNames="$(InternalsAssemblyExcludeTypeNames)" UseEmptyMethodBodies="$(InternalsAssemblyUseEmptyMethodBodies)">
<Output ItemName="ReferencePath" TaskParameter="TargetReferences" />
<Output ItemName="_IACTG_RemovedReferences" TaskParameter="RemovedReferences" />
<Output ItemName="Compile" TaskParameter="GeneratedCodeFiles" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<NuspecFile>IgnoresAccessChecksToGenerator.nuspec</NuspecFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?xml version="1.0"?>
<package >
<package>
<metadata>
<id>IgnoresAccessChecksToGenerator</id>
<version>0.4.0</version>
<version>0.5.0</version>
<authors>aelij</authors>
<owners>aelij</owners>
<licenseUrl>https://github.com/aelij/IgnoresAccessChecksToGenerator/blob/master/LICENSE</licenseUrl>
<license type="expression">MIT</license>
<projectUrl>https://github.com/aelij/IgnoresAccessChecksToGenerator</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Generates IgnoresAccessChecksTo attributes and reference assemblies to allow compile-time access to internals</description>
<releaseNotes></releaseNotes>
<copyright>Copyright 2017</copyright>
<copyright>Copyright 2021</copyright>
<tags>IgnoresAccessChecksToGenerator internals</tags>
</metadata>
<files>
<file src="IgnoresAccessChecksToGenerator.Tasks\bin\Release\**\*.dll" target="tools" />
<file src="IgnoresAccessChecksToGenerator.Tasks\bin\Release\**\*.deps.json" target="tools" />
<file src="Build\**" target="build" />
<file src="bin\Release\**\*.dll" target="tools" />
<file src="bin\Release\**\*.deps.json" target="tools" />
<file src="..\Build\**" target="build" />
</files>
</package>
11 changes: 9 additions & 2 deletions src/IgnoresAccessChecksToGenerator.Tasks/PublicizeInternals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down Expand Up @@ -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 ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>Exe</OutputType>
<InternalsAssemblyNames>Microsoft.CodeAnalysis;Microsoft.CodeAnalysis.CSharp</InternalsAssemblyNames>
<InternalsAssemblyUseEmptyMethodBodies>false</InternalsAssemblyUseEmptyMethodBodies>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="IgnoresAccessChecksToGenerator" Version="0.2.0" PrivateAssets="all" />
<PackageReference Include="IgnoresAccessChecksToGenerator" Version="0.5.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.3.1" />
</ItemGroup>

Expand Down

0 comments on commit 83db587

Please sign in to comment.