Skip to content

Commit

Permalink
Fix Proxy creation failure for explicit generic interface (#3034)
Browse files Browse the repository at this point in the history
Fix #3029
  • Loading branch information
fredericDelaporte authored Mar 20, 2022
1 parent 211891f commit 2344bd8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ protected override HbmMapping GetMappings()
rc.Property(x => x.Name);
});

mapper.Class<EntityWithExplicitGenericInterface>(
rc =>
{
rc.Table("explGenInterface");
rc.Id(x => x.Id);
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

Expand Down Expand Up @@ -177,6 +184,15 @@ public async Task ProxyInterfaceIdAccessFromDifferentInterfacesAsync()
}
}

[Test]
public void ProxyExplicitGenericInterfaceAsync()
{
using (var session = OpenSession())
{
Assert.That(() => session.LoadAsync<EntityWithExplicitGenericInterface>(_id), Throws.Nothing, "Failed to load EntityWithExplicitGenericInterface proxy");
}
}

private void ThrowOnIEntityNameAccess(IEntity entity)
{
Assert.That(() => entity.Name, Throws.TypeOf<ObjectNotFoundException>(), "IEntity.Name access should lead to proxy initialization");
Expand Down
15 changes: 15 additions & 0 deletions src/NHibernate.Test/StaticProxyTest/InterfaceHandling/Entities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,19 @@ public class EntityWithSuperClassInterfaceLookup

public virtual IEntity EntityLookup { get; set; }
}

public class EntityWithExplicitGenericInterface : IGeneric
{
public virtual Guid Id { get; set; }

T[] IGeneric.GetTypes<T>()
{
return null;
}
}

public interface IGeneric
{
T[] GetTypes<T>();
}
}
16 changes: 16 additions & 0 deletions src/NHibernate.Test/StaticProxyTest/InterfaceHandling/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ protected override HbmMapping GetMappings()
rc.Property(x => x.Name);
});

mapper.Class<EntityWithExplicitGenericInterface>(
rc =>
{
rc.Table("explGenInterface");
rc.Id(x => x.Id);
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

Expand Down Expand Up @@ -166,6 +173,15 @@ public void ProxyInterfaceIdAccessFromDifferentInterfaces()
}
}

[Test]
public void ProxyExplicitGenericInterface()
{
using (var session = OpenSession())
{
Assert.That(() => session.Load<EntityWithExplicitGenericInterface>(_id), Throws.Nothing, "Failed to load EntityWithExplicitGenericInterface proxy");
}
}

private void ThrowOnIEntityNameAccess(IEntity entity)
{
Assert.That(() => entity.Name, Throws.TypeOf<ObjectNotFoundException>(), "IEntity.Name access should lead to proxy initialization");
Expand Down
5 changes: 3 additions & 2 deletions src/NHibernate/Proxy/ProxyBuilderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ internal static MethodBuilder GenerateMethodSignature(string name, MethodInfo me
CallingConventions.HasThis,
method.ReturnType,
parameters.ToArray(param => param.ParameterType));
if (explicitImplementation)
methodBuilder.SetImplementationFlags(MethodImplAttributes.Managed | MethodImplAttributes.IL);

var typeArgs = method.GetGenericArguments();
if (typeArgs.Length > 0)
Expand Down Expand Up @@ -208,6 +206,9 @@ internal static MethodBuilder GenerateMethodSignature(string name, MethodInfo me
}
}

if (explicitImplementation)
methodBuilder.SetImplementationFlags(MethodImplAttributes.Managed | MethodImplAttributes.IL);

return methodBuilder;
}

Expand Down

0 comments on commit 2344bd8

Please sign in to comment.