Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mob-sakai/opensesame

A custom Roslyn compiler that allows access to internals/privates in other assemblies. Say "Open, Sesame!"
https://github.com/mob-sakai/opensesame

accessibility compiler csc csharp roslyn

Last synced: about 2 months ago
JSON representation

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

Awesome Lists containing this project

README

        

Open Sesame
===

A custom [Roslyn](https://github.com/dotnet/roslyn) compiler **that allows access to internals/privates in other assemblies.**

![License](https://img.shields.io/github/license/mob-sakai/OpenSesame)

![.NetFramework](https://img.shields.io/static/v1?label=.Net+Framework&message=4.7+or+later&color=blue)
![.NetCore](https://img.shields.io/static/v1?label=.Net+Core&message=2.0+or+later&color=red)
![.NetStandard](https://img.shields.io/static/v1?label=.Net+Standard&message=2.0+or+later&color=orange)

![test](https://github.com/mob-sakai/OpenSesame/workflows/test/badge.svg)
![release](https://github.com/mob-sakai/OpenSesame/workflows/release/badge.svg)

## Description

This package contains a custom [Roslyn](https://github.com/dotnet/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][reflection].**

Have you ever heard of a secret attribute `IgnoresAccessChecksToAttribute`?
For more information, See [the great article by Filip W][ignores-access].

[reflection]: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection
[ignores-access]: https://www.strathweb.com/2018/10/no-internalvisibleto-no-problem-bypassing-c-visibility-rules-with-roslyn/

### 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][] |

[OpenSesame.Net.Compilers.Toolset]: https://www.nuget.org/packages/OpenSesame.Net.Compilers.Toolset
[Microsoft.Net.Compilers.Toolset]: https://www.nuget.org/packages/Microsoft.Net.Compilers.Toolset
[V1]: https://img.shields.io/nuget/v/OpenSesame.Net.Compilers.Toolset
[D1]: https://img.shields.io/nuget/dt/OpenSesame.Net.Compilers.Toolset
[MV1]: https://img.shields.io/nuget/v/Microsoft.Net.Compilers.Toolset

[OpenSesame.Net.Compilers]: https://www.nuget.org/packages/OpenSesame.Net.Compilers
[Microsoft.Net.Compilers]: https://www.nuget.org/packages/Microsoft.Net.Compilers
[V2]: https://img.shields.io/nuget/v/OpenSesame.Net.Compilers
[D2]: https://img.shields.io/nuget/dt/OpenSesame.Net.Compilers

[OpenSesame.NetCore.Compilers]: https://www.nuget.org/packages/OpenSesame.NetCore.Compilers
[Microsoft.NetCore.Compilers]: https://www.nuget.org/packages/Microsoft.NetCore.Compilers
[V3]: https://img.shields.io/nuget/v/OpenSesame.NetCore.Compilers
[D3]: https://img.shields.io/nuget/dt/OpenSesame.NetCore.Compilers





## 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:
```cs
[assembly: System.Runtime.CompilerServices.IgnoresAccessChecksTo("")]
```
3. The accessibility of the symbols (types, methods, properties, etc.) contained in the `` will be ignored.

### For Unity

Use a unity package [com.coffee.open-sesame-compiler](https://github.com/mob-sakai/OpenSesameCompilerForUnity).





## 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][code] is compiled by `OpenSesame.Net.Compilers.Toolset`, the changes are as follows:

[code]: https://github.com/mob-sakai/OpenSesame/blob/main/Tests/PrivateLibrary.Console/Program.cs

```diff
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][issue_template]

[issue_template]: https://github.com/mob-sakai/OpenSesame/issues/new?assignees=mob-sakai&template=update_roslyn.md&title=Request+to+update+roslyn%3A+%7Bversion%7D

### 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:

```bash
./tool.sh --pack --run-tests --publish --token
```