Skip to content

Commit 9255435

Browse files
authored
Add support for internal entity classes by proxy factory (#2568)
1 parent 179056e commit 9255435

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/NHibernate.Test/StaticProxyTest/StaticProxyFactoryFixture.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ public class InternalInterfaceTestClass : IInternal
3737
public virtual int Id { get; set; }
3838
}
3939

40+
[Serializable]
41+
internal class InternalTestClass
42+
{
43+
int Id { get; set; }
44+
string Name { get; set; }
45+
}
46+
4047
public interface IPublic
4148
{
4249
int Id { get; set; }
@@ -517,6 +524,32 @@ public void VerifyProxyForAbstractClass()
517524
Assert.That(proxy, Is.Not.Null);
518525
Assert.That(proxy, Is.InstanceOf<IPublic>());
519526
Assert.That(proxy, Is.InstanceOf<AbstractTestClass>());
527+
#if NETFX
528+
});
529+
#endif
530+
}
531+
532+
[Test]
533+
public void VerifyProxyForInternalClass()
534+
{
535+
var factory = new StaticProxyFactory();
536+
factory.PostInstantiate(
537+
typeof(InternalTestClass).FullName,
538+
typeof(InternalTestClass),
539+
new HashSet<System.Type> { typeof(INHibernateProxy) },
540+
null, null, null, true);
541+
542+
#if NETFX
543+
VerifyGeneratedAssembly(
544+
() =>
545+
{
546+
#endif
547+
var proxy = factory.GetProxy(1, null);
548+
Assert.That(proxy, Is.Not.Null);
549+
Assert.That(proxy, Is.InstanceOf<InternalTestClass>());
550+
551+
Assert.That(factory.GetFieldInterceptionProxy(), Is.InstanceOf<InternalTestClass>());
552+
520553
#if NETFX
521554
});
522555
#endif

src/NHibernate/Proxy/FieldInterceptorProxyBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public static TypeInfo CreateProxyType(System.Type baseType)
5656
var name = new AssemblyName(assemblyName);
5757

5858
var assemblyBuilder = ProxyBuilderHelper.DefineDynamicAssembly(AppDomain.CurrentDomain, name);
59+
60+
#if NETFX || NETCOREAPP2_0
61+
if (!baseType.IsVisible)
62+
ProxyBuilderHelper.GenerateInstanceOfIgnoresAccessChecksToAttribute(assemblyBuilder, baseType.Assembly.GetName().Name);
63+
#endif
5964
var moduleBuilder = ProxyBuilderHelper.DefineDynamicModule(assemblyBuilder, moduleName);
6065

6166
const TypeAttributes typeAttributes = TypeAttributes.AutoClass | TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.BeforeFieldInit;

src/NHibernate/Proxy/NHibernateProxyBuilder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ public TypeInfo CreateProxyType(System.Type baseType, IReadOnlyCollection<System
7878

7979
#if NETFX || NETCOREAPP2_0
8080
var assemblyNamesToIgnoreAccessCheck =
81-
interfaces.Where(i => !i.IsVisible)
82-
.Select(i => i.Assembly.GetName().Name)
83-
.Distinct();
81+
new[] {baseType}
82+
.Concat(interfaces).Where(i => !i.IsVisible)
83+
.Select(i => i.Assembly.GetName().Name)
84+
.Distinct();
8485
foreach (var a in assemblyNamesToIgnoreAccessCheck)
8586
ProxyBuilderHelper.GenerateInstanceOfIgnoresAccessChecksToAttribute(assemblyBuilder, a);
8687
#else

0 commit comments

Comments
 (0)