Skip to content

Commit 33f90d3

Browse files
authored
Fix InvalidOperationException in ProxyGenerator for class with generic non-virtual method (nhibernate#2620)
Order of operations in ProxyBuilderHelper changed which is relevant for generic methods Fixes nhibernate#2619
1 parent 58e3cb2 commit 33f90d3

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/NHibernate.Test/StaticProxyTest/StaticProxyFactoryFixture.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,22 @@ protected virtual void GetObjectData(SerializationInfo info, StreamingContext co
209209
}
210210
}
211211

212+
[Serializable]
213+
public abstract class ClassWithGenericNonVirtualMethod : IWithGenericMethod
214+
{
215+
public virtual int Id { get; set; }
216+
public virtual string Name { get; set; }
217+
public T CopyTo<T>() where T : class, IWithGenericMethod
218+
{
219+
return null;
220+
}
221+
}
222+
223+
public interface IWithGenericMethod
224+
{
225+
T CopyTo<T>() where T : class, IWithGenericMethod;
226+
}
227+
212228
[Test]
213229
public void VerifyProxyForClassWithInternalInterface()
214230
{
@@ -550,6 +566,32 @@ public void VerifyProxyForInternalClass()
550566

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

569+
#if NETFX
570+
});
571+
#endif
572+
}
573+
574+
[Test(Description = "GH2619")]
575+
public void VerifyProxyForClassWithGenericNonVirtualMethod()
576+
{
577+
var factory = new StaticProxyFactory();
578+
factory.PostInstantiate(
579+
typeof(ClassWithGenericNonVirtualMethod).FullName,
580+
typeof(ClassWithGenericNonVirtualMethod),
581+
new HashSet<System.Type> { typeof(INHibernateProxy) },
582+
null, null, null, true);
583+
584+
#if NETFX
585+
VerifyGeneratedAssembly(
586+
() =>
587+
{
588+
#endif
589+
var proxy = factory.GetProxy(1, null);
590+
Assert.That(proxy, Is.Not.Null);
591+
Assert.That(proxy, Is.InstanceOf<ClassWithGenericNonVirtualMethod>());
592+
593+
Assert.That(factory.GetFieldInterceptionProxy(), Is.InstanceOf<ClassWithGenericNonVirtualMethod>());
594+
553595
#if NETFX
554596
});
555597
#endif

src/NHibernate/Proxy/ProxyBuilderHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,6 @@ internal static MethodBuilder GenerateMethodSignature(string name, MethodInfo me
178178
CallingConventions.HasThis,
179179
method.ReturnType,
180180
parameters.ToArray(param => param.ParameterType));
181-
if (explicitImplementation)
182-
methodBuilder.SetImplementationFlags(MethodImplAttributes.Managed | MethodImplAttributes.IL);
183181

184182
var typeArgs = method.GetGenericArguments();
185183
if (typeArgs.Length > 0)
@@ -208,6 +206,9 @@ internal static MethodBuilder GenerateMethodSignature(string name, MethodInfo me
208206
}
209207
}
210208

209+
if (explicitImplementation)
210+
methodBuilder.SetImplementationFlags(MethodImplAttributes.Managed | MethodImplAttributes.IL);
211+
211212
return methodBuilder;
212213
}
213214

0 commit comments

Comments
 (0)