Skip to content

A custom Roslyn compiler that allows access to internals/privates in other assemblies. Say "Open, Sesame!"

License

Notifications You must be signed in to change notification settings

mob-sakai/OpenSesame

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Open Sesame

A custom Roslyn compiler that allows access to internals/privates in other assemblies.

License

.NetFramework .NetCore .NetStandard

test release

Description

This package contains a custom Roslyn compiler. If the package is installed in a c# project, it will override the compiler used in the build.

The custom compiler automatically injects IgnoresAccessChecksToAttribute to the assembly. This attribute ignores accessibility to the assembly with the given name. In other words, you can access to internals/privates in other assemblies, without reflection feature.

Have you ever heard of a secret attribute IgnoresAccessChecksToAttribute? For more information, See the great article by Filip W.

For "bridge" assembly

If you use an assembly (*.dll) generated by the OpenSesame compiler in another project, you do not need to use the OpenSesame compiler again.

This means that a "bridge" assembly to another assembly can be easily generated.

Changes from original roslyn

  • Change the names of the packages Microsoft.* to OpenSesame.*
  • Allow unsafe code automatically (for .Net Framework)
  • With MetadataImportOptions.All for compilation options
  • With BinderFlags.IgnoreAccessibility for compilation options
  • Inject IgnoresAccessChecksToAttribute class automatically

Supported frameworks

  • Support C# 8.0
  • .Net Framework 4.7 or later
  • .Net Core 2.0 or later
  • .Net Standard 2.0 or later
  • Unity 2018.3 or later





Packages

Package Name Original Package Version Downloads
OpenSesame.Net.Compilers.Toolset Microsoft.Net.Compilers.Toolset V1 D1
OpenSesame.Net.Compilers (deprecated) Microsoft.Net.Compilers V2 D2
OpenSesame.NetCore.Compilers (deprecated) Microsoft.NetCore.Compilers V3 D3





Usage

For C# project (.Net Framework)

For a C# project (*.csproj), you can install the Toolset package to change the compiler to be used at build time.

  1. Install the nuget package OpenSesame.Net.Compilers.Toolset to the project.
  2. The accessibility of all symbols (types, methods, properties, etc.) will be ignored.

For C# project (.Net Core or .Net Standard)

For a C# project (*.csproj), you can install the Toolset package to change the compiler to be used at build time.

  1. Install the nuget package OpenSesame.Net.Compilers.Toolset to the project.
  2. Adddd the following somewhere in your code:
[assembly: System.Runtime.CompilerServices.IgnoresAccessChecksTo("<TargetAssemblyName>")]
  1. The accessibility of the symbols (types, methods, properties, etc.) contained in the <TargetAssemblyName> will be ignored.

For Unity

Use a unity package com.coffee.open-sesame-compiler.





Unsupported Features

Set/get value into readonly field

Use reflection

IDE support

Use Csc-Manager to modify your VisualStudio Code and C# extension.

  • csc-manager enable-vscode: Show internals/privates in other assembly.
  • csc-manager disable-vscode: Hide them.





How does the OpenSesame compiler affect the generated assemblies?

When Program.cs is compiled by OpenSesame.Net.Compilers.Toolset, the changes are as follows:

using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
+using System.Security;
+using System.Security.Permissions;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
+[assembly: IgnoresAccessChecksTo("PrivateLibrary")]
[assembly: TargetFramework(".NETCoreApp,Version=v3.1", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("PrivateLibrary.Console")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("PrivateLibrary.Console")]
[assembly: AssemblyTitle("PrivateLibrary.Console")]
+[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
+[module: UnverifiableCode]
+namespace System.Runtime.CompilerServices
+{
+	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
+	public class IgnoresAccessChecksToAttribute : Attribute
+	{
+		public string AssemblyName
+		{
+			get;
+		}
+
+		public IgnoresAccessChecksToAttribute(string assemblyName)
+		{
+			AssemblyName = assemblyName;
+		}
+	}
+}
namespace PrivateLibrary.Console
{
	internal class Program
	{
		private static void Main(string[] args)
		{
			System.Console.WriteLine(new InternalClass().privateStringField);
		}
	}
}





Develop

Update Roslyn version

OpenSesame Roslyn
V1 MV1

If a new stable version of the Rosly package has been released, please update Roslyn.

Create an issue using the Roslyn update request issue template

Run Tests

./tool.sh --pack --run-tests

Release

When push to main or v*.x branch, this package is automatically released by GitHub Action.

  • Update version of the package
  • Update and push CHANGELOG.md
  • Create version tag
  • Release on GitHub
  • Publish to nuget registory

Alternatively, publish packages manually with the following command:

./tool.sh --pack --run-tests --publish --token <NUGET_TOKEN>