-
-
Save jkotas/ddfeb9ec47bff90b4249534c4cd0761a to your computer and use it in GitHub Desktop.
IgnoreAccessibilitySample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> | |
<InlineIL /> | |
</Weavers> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace System.Runtime.CompilerServices | |
{ | |
public class IgnoresAccessChecksToAttribute : Attribute | |
{ | |
public IgnoresAccessChecksToAttribute(string assemblyName) | |
{ | |
AssemblyName = assemblyName; | |
} | |
public string AssemblyName { get; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net7.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Fody" Version="6.6.4" PrivateAssets="all" /> | |
<PackageReference Include="InlineIL.Fody" Version="1.7.2" PrivateAssets="all" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using InlineIL; | |
using static InlineIL.IL.Emit; | |
// Ignore runtime access checks to itself | |
[assembly: System.Runtime.CompilerServices.IgnoresAccessChecksTo("IgnoreAccessibilitySample")] | |
Console.WriteLine(GetPrivateFieldFromUserDefinedType(new UserDefinedType(false))); | |
Console.WriteLine(GetPrivateFieldFromUserDefinedType(new UserDefinedType(true))); | |
static bool GetPrivateFieldFromUserDefinedType(UserDefinedType u) | |
{ | |
Ldarg_0(); | |
Ldfld(new FieldRef(typeof(UserDefinedType), "_privateField")); | |
return IL.Return<bool>(); | |
} | |
// ---------------- | |
class UserDefinedType | |
{ | |
bool _privateField; | |
public UserDefinedType(bool v) | |
{ | |
_privateField = v; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment