Home

Awesome

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

Supported frameworks

<br><br><br><br>

Packages

Package NameOriginal PackageVersionDownloads
OpenSesame.Net.Compilers.ToolsetMicrosoft.Net.Compilers.ToolsetV1D1
OpenSesame.Net.Compilers (deprecated)Microsoft.Net.CompilersV2D2
OpenSesame.NetCore.Compilers (deprecated)Microsoft.NetCore.CompilersV3D3

<br><br><br><br>

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.

<br><br><br><br>

Unsupported Features

Set/get value into readonly field

Use reflection

IDE support

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

<br><br><br><br>

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);
		}
	}
}

<br><br><br><br>

Develop

Update Roslyn version

OpenSesameRoslyn
V1MV1

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.

Alternatively, publish packages manually with the following command:

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