Skip to content

Commit 8b7952a

Browse files
authored
Fix TypeLoadException on proxies of classes with init properties (nhibernate#3189)
1 parent f50c07d commit 8b7952a

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

.config/dotnet-tools.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"isRoot": true,
44
"tools": {
55
"csharpasyncgenerator.tool": {
6-
"version": "0.20.1",
6+
"version": "0.21.1",
77
"commands": [
88
"async-generator"
99
]
@@ -15,4 +15,4 @@
1515
]
1616
}
1717
}
18-
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#if !NET5_0_OR_GREATER
2+
using System.ComponentModel;
3+
4+
// ReSharper disable once CheckNamespace
5+
namespace System.Runtime.CompilerServices
6+
{
7+
/// <summary>
8+
/// Reserved to be used by the compiler for tracking metadata.
9+
/// This class should not be used by developers in source code.
10+
/// </summary>
11+
[EditorBrowsable(EditorBrowsableState.Never)]
12+
internal static class IsExternalInit
13+
{
14+
}
15+
}
16+
#endif

src/NHibernate.Test/StaticProxyTest/StaticProxyFactoryFixture.cs

+33
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ public interface IWithGenericMethod
225225
T CopyTo<T>() where T : class, IWithGenericMethod;
226226
}
227227

228+
[Serializable]
229+
public class ClassWithInitProperties
230+
{
231+
public virtual int Id { get; init; }
232+
public virtual string Name { get; init; }
233+
}
234+
228235
[Test]
229236
public void VerifyProxyForClassWithInternalInterface()
230237
{
@@ -592,6 +599,32 @@ public void VerifyProxyForClassWithGenericNonVirtualMethod()
592599

593600
Assert.That(factory.GetFieldInterceptionProxy(), Is.InstanceOf<ClassWithGenericNonVirtualMethod>());
594601

602+
#if NETFX
603+
});
604+
#endif
605+
}
606+
607+
[Test]
608+
public void VerifyProxyForClassWithInitProperties()
609+
{
610+
var factory = new StaticProxyFactory();
611+
factory.PostInstantiate(
612+
typeof(ClassWithInitProperties).FullName,
613+
typeof(ClassWithInitProperties),
614+
new HashSet<System.Type> { typeof(INHibernateProxy) },
615+
null, null, null, true);
616+
617+
#if NETFX
618+
VerifyGeneratedAssembly(
619+
() =>
620+
{
621+
#endif
622+
var proxy = factory.GetProxy(1, null);
623+
Assert.That(proxy, Is.Not.Null);
624+
Assert.That(proxy, Is.InstanceOf<ClassWithInitProperties>());
625+
626+
Assert.That(factory.GetFieldInterceptionProxy(), Is.InstanceOf<ClassWithInitProperties>());
627+
595628
#if NETFX
596629
});
597630
#endif

src/NHibernate/Proxy/ProxyBuilderHelper.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,18 @@ internal static MethodBuilder GenerateMethodSignature(string name, MethodInfo me
171171
var implementationName = explicitImplementation
172172
? $"{method.DeclaringType.FullName}.{name}"
173173
: name;
174+
174175
var methodBuilder =
175176
typeBuilder.DefineMethod(
176177
implementationName,
177178
methodAttributes,
178179
CallingConventions.HasThis,
179180
method.ReturnType,
180-
parameters.ToArray(param => param.ParameterType));
181+
method.ReturnParameter?.GetRequiredCustomModifiers(),
182+
method.ReturnParameter?.GetOptionalCustomModifiers(),
183+
parameters.ToArray(p => p.ParameterType),
184+
parameters.ToArray(p => p.GetRequiredCustomModifiers()),
185+
parameters.ToArray(p => p.GetOptionalCustomModifiers()));
181186

182187
var typeArgs = method.GetGenericArguments();
183188
if (typeArgs.Length > 0)

0 commit comments

Comments
 (0)