Skip to content

Commit 26f0af4

Browse files
fixup! Implement a static IFieldInterceptorAccessor
1 parent f209b1b commit 26f0af4

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/NHibernate.Test/StaticProxyTest/StaticProxyFactoryFixture.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,34 @@ private static void VerifyGeneratedAssembly(System.Action assemblyGenerator)
6161
new PeVerifier(assemblyName).AssertIsValid();
6262
}
6363
#endif
64+
65+
public interface IPublicTest
66+
{
67+
int Id { get; }
68+
}
69+
70+
public class PublicInterfaceTestClass : IPublicTest
71+
{
72+
public virtual int Id { get; set; }
73+
}
74+
75+
[Test]
76+
public void CanGenerateValidFieldInterceptorProxyWithAdditionalInterface()
77+
{
78+
var factory = new StaticProxyFactory();
79+
factory.PostInstantiate(
80+
typeof(TestClass).FullName,
81+
typeof(TestClass),
82+
// By way of the "proxy" attribute on the "class" mapping, an interface to use for the
83+
// lazy entity load proxy instead of the persistentClass can be specified. This is "translated" into
84+
// having an additional interface in the interface list, instead of just having INHibernateProxy.
85+
// (Quite a loosy semantic...)
86+
// The field interceptor proxy ignores this setting, as it does not delegate its implementation
87+
// to an instance of the persistentClass, and so cannot implement interface methods.
88+
new HashSet<System.Type> {typeof(INHibernateProxy), typeof(IPublicTest)},
89+
null, null, null);
90+
var fieldProxy = factory.GetFieldInterceptionProxy(null);
91+
Assert.That(fieldProxy, Is.InstanceOf<TestClass>());
92+
}
6493
}
6594
}

src/NHibernate/Proxy/FieldInterceptorProxyBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ public static TypeInfo CreateProxyType(System.Type baseType)
4747
var parentType = baseType;
4848
if (baseType.IsInterface)
4949
{
50-
parentType = typeof(object);
51-
interfaces.Add(baseType);
50+
throw new ArgumentException(
51+
$"Field interceptor proxy does not support being build on an interface baseType ({baseType.FullName}).",
52+
nameof(baseType));
5253
}
5354

5455
interfaces.RemoveWhere(i => !i.IsVisible);

src/NHibernate/Proxy/StaticProxyFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy> Cre
4444

4545
public override object GetFieldInterceptionProxy(object instanceToWrap)
4646
{
47-
var cacheEntry = new ProxyCacheEntry(IsClassProxy ? PersistentClass : typeof(object), System.Type.EmptyTypes);
47+
var cacheEntry = new ProxyCacheEntry(PersistentClass, System.Type.EmptyTypes);
4848
var proxyActivator = FieldInterceptorCache.GetOrAdd(cacheEntry, CreateFieldInterceptionProxyActivator);
4949
return proxyActivator();
5050
}

0 commit comments

Comments
 (0)