Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark proxy assembly with IgnoresAccessChecksToAttribute to allow implementing non public interfaces #1814

Merged
merged 1 commit into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/NHibernate/Proxy/NHibernateProxyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,17 @@ public TypeInfo CreateProxyType(System.Type baseType, IReadOnlyCollection<System
interfaces.Add(baseType);
}

#if NETFX || NETCOREAPP2_0
var assemblyNamesToIgnoreAccessCheck =
interfaces.Where(i => !i.IsVisible)
.Select(i => i.Assembly.GetName().Name)
.Distinct();
foreach (var a in assemblyNamesToIgnoreAccessCheck)
ProxyBuilderHelper.GenerateInstanceOfIgnoresAccessChecksToAttribute(assemblyBuilder, a);
#else
interfaces.RemoveWhere(i => !i.IsVisible);

#endif

var typeBuilder = moduleBuilder.DefineType(typeName, typeAttributes, parentType, interfaces.ToArray());

var lazyInitializerField = typeBuilder.DefineField("__lazyInitializer", LazyInitializerType, FieldAttributes.Private);
Expand Down
14 changes: 14 additions & 0 deletions src/NHibernate/Proxy/ProxyBuilderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Security;
using NHibernate.Util;
Expand All @@ -21,6 +22,7 @@ internal static class ProxyBuilderHelper
{
private static readonly ConstructorInfo ObjectConstructor = typeof(object).GetConstructor(System.Type.EmptyTypes);
private static readonly ConstructorInfo SecurityCriticalAttributeConstructor = typeof(SecurityCriticalAttribute).GetConstructor(System.Type.EmptyTypes);
private static readonly ConstructorInfo IgnoresAccessChecksToAttributeConstructor = typeof(IgnoresAccessChecksToAttribute).GetConstructor(new[] {typeof(string)});

internal static readonly MethodInfo SerializableGetObjectDataMethod = typeof(ISerializable).GetMethod(nameof(ISerializable.GetObjectData));
internal static readonly MethodInfo SerializationInfoSetTypeMethod = ReflectHelper.GetMethod<SerializationInfo>(si => si.SetType(null));
Expand Down Expand Up @@ -181,6 +183,18 @@ internal static MethodBuilder GenerateMethodSignature(string name, MethodInfo me
return methodBuilder;
}

internal static void GenerateInstanceOfIgnoresAccessChecksToAttribute(
AssemblyBuilder assemblyBuilder,
string assemblyName)
{
// [assembly: System.Runtime.CompilerServices.IgnoresAccessChecksToAttribute(assemblyName)]
var customAttributeBuilder = new CustomAttributeBuilder(
IgnoresAccessChecksToAttributeConstructor,
new object[] {assemblyName});

assemblyBuilder.SetCustomAttribute(customAttributeBuilder);
}

private static System.Type ResolveTypeConstraint(MethodInfo method, System.Type typeConstraint)
{
if (typeConstraint != null && typeConstraint.IsGenericType)
Expand Down
14 changes: 14 additions & 0 deletions src/NHibernate/Util/IgnoresAccessChecksToAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// ReSharper disable once CheckNamespace
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
public class IgnoresAccessChecksToAttribute : Attribute
{
public string AssemblyName { get; }

public IgnoresAccessChecksToAttribute(string assemblyName)
{
AssemblyName = assemblyName;
}
}
}