Skip to content

Support proxies of classes with init properties #3189

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 4 commits into from
Oct 30, 2022
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
4 changes: 2 additions & 2 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"csharpasyncgenerator.tool": {
"version": "0.20.1",
"version": "0.21.1",
"commands": [
"async-generator"
]
Expand All @@ -15,4 +15,4 @@
]
}
}
}
}
16 changes: 16 additions & 0 deletions src/NHibernate.Test/StaticProxyTest/IsExternalInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#if !NET5_0_OR_GREATER
using System.ComponentModel;

// ReSharper disable once CheckNamespace
namespace System.Runtime.CompilerServices
{
/// <summary>
/// Reserved to be used by the compiler for tracking metadata.
/// This class should not be used by developers in source code.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal static class IsExternalInit
{
}
}
#endif
33 changes: 33 additions & 0 deletions src/NHibernate.Test/StaticProxyTest/StaticProxyFactoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ public interface IWithGenericMethod
T CopyTo<T>() where T : class, IWithGenericMethod;
}

[Serializable]
public class ClassWithInitProperties
{
public virtual int Id { get; init; }
public virtual string Name { get; init; }
}

[Test]
public void VerifyProxyForClassWithInternalInterface()
{
Expand Down Expand Up @@ -592,6 +599,32 @@ public void VerifyProxyForClassWithGenericNonVirtualMethod()

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

#if NETFX
});
#endif
}

[Test]
public void VerifyProxyForClassWithInitProperties()
{
var factory = new StaticProxyFactory();
factory.PostInstantiate(
typeof(ClassWithInitProperties).FullName,
typeof(ClassWithInitProperties),
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<ClassWithInitProperties>());

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

#if NETFX
});
#endif
Expand Down
7 changes: 6 additions & 1 deletion src/NHibernate/Proxy/ProxyBuilderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,18 @@ internal static MethodBuilder GenerateMethodSignature(string name, MethodInfo me
var implementationName = explicitImplementation
? $"{method.DeclaringType.FullName}.{name}"
: name;

var methodBuilder =
typeBuilder.DefineMethod(
implementationName,
methodAttributes,
CallingConventions.HasThis,
method.ReturnType,
parameters.ToArray(param => param.ParameterType));
method.ReturnParameter?.GetRequiredCustomModifiers(),
method.ReturnParameter?.GetOptionalCustomModifiers(),
parameters.ToArray(p => p.ParameterType),
parameters.ToArray(p => p.GetRequiredCustomModifiers()),
parameters.ToArray(p => p.GetOptionalCustomModifiers()));

var typeArgs = method.GetGenericArguments();
if (typeArgs.Length > 0)
Expand Down