Skip to content

Support internal entity classes by proxy factory #2568

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

Merged
merged 2 commits into from
Sep 28, 2020
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
33 changes: 33 additions & 0 deletions src/NHibernate.Test/StaticProxyTest/StaticProxyFactoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public class InternalInterfaceTestClass : IInternal
public virtual int Id { get; set; }
}

[Serializable]
internal class InternalTestClass
{
int Id { get; set; }
string Name { get; set; }
}

public interface IPublic
{
int Id { get; set; }
Expand Down Expand Up @@ -517,6 +524,32 @@ public void VerifyProxyForAbstractClass()
Assert.That(proxy, Is.Not.Null);
Assert.That(proxy, Is.InstanceOf<IPublic>());
Assert.That(proxy, Is.InstanceOf<AbstractTestClass>());
#if NETFX
});
#endif
}

[Test]
public void VerifyProxyForInternalClass()
{
var factory = new StaticProxyFactory();
factory.PostInstantiate(
typeof(InternalTestClass).FullName,
typeof(InternalTestClass),
new HashSet<System.Type> { typeof(INHibernateProxy) },
null, null, null, true);

#if NETFX
VerifyGeneratedAssembly(
() =>
{
#endif
var proxy = factory.GetProxy(1, null);
Assert.That(proxy, Is.Not.Null);
Assert.That(proxy, Is.InstanceOf<InternalTestClass>());

Assert.That(factory.GetFieldInterceptionProxy(), Is.InstanceOf<InternalTestClass>());

#if NETFX
});
#endif
Expand Down
5 changes: 5 additions & 0 deletions src/NHibernate/Proxy/FieldInterceptorProxyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public static TypeInfo CreateProxyType(System.Type baseType)
var name = new AssemblyName(assemblyName);

var assemblyBuilder = ProxyBuilderHelper.DefineDynamicAssembly(AppDomain.CurrentDomain, name);

#if NETFX || NETCOREAPP2_0
if (!baseType.IsVisible)
ProxyBuilderHelper.GenerateInstanceOfIgnoresAccessChecksToAttribute(assemblyBuilder, baseType.Assembly.GetName().Name);
#endif
var moduleBuilder = ProxyBuilderHelper.DefineDynamicModule(assemblyBuilder, moduleName);

const TypeAttributes typeAttributes = TypeAttributes.AutoClass | TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.BeforeFieldInit;
Expand Down
7 changes: 4 additions & 3 deletions src/NHibernate/Proxy/NHibernateProxyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ public TypeInfo CreateProxyType(System.Type baseType, IReadOnlyCollection<System

#if NETFX || NETCOREAPP2_0
var assemblyNamesToIgnoreAccessCheck =
interfaces.Where(i => !i.IsVisible)
.Select(i => i.Assembly.GetName().Name)
.Distinct();
new[] {baseType}
.Concat(interfaces).Where(i => !i.IsVisible)
.Select(i => i.Assembly.GetName().Name)
.Distinct();
foreach (var a in assemblyNamesToIgnoreAccessCheck)
ProxyBuilderHelper.GenerateInstanceOfIgnoresAccessChecksToAttribute(assemblyBuilder, a);
#else
Expand Down