Skip to content

Fix TypeLoadException in StaticProxyFactory when class has not visible interfaces #1644

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 1 commit into from
Apr 8, 2018
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
28 changes: 28 additions & 0 deletions src/NHibernate.Test/StaticProxyTest/StaticProxyFactoryFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;
using NHibernate.Proxy;
using NUnit.Framework;

namespace NHibernate.Test.StaticProxyTest
{
public class StaticProxyFactoryFixture
{
internal interface ISomething
{
int Id { get; }
}

public class TestClass : ISomething
{
public virtual int Id { get; set; }
}

[Test]
public void CanCreateProxyForClassWithInternalInterface()
{
var factory = new StaticProxyFactory();
factory.PostInstantiate(typeof(TestClass).FullName, typeof(TestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
var proxy = factory.GetProxy(1, null);
Assert.That(proxy, Is.Not.Null);
}
}
}
2 changes: 2 additions & 0 deletions src/NHibernate/Proxy/NHibernateProxyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public TypeInfo CreateProxyType(System.Type baseType, IReadOnlyCollection<System
interfaces.Add(baseType);
}

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

Copy link
Member

@fredericDelaporte fredericDelaporte Apr 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it emit a warning there?
It is not the dynamic proxy (which should be removed once lazy properties are handled by static proxies).

Something like "Interface {0} is not accessible, its members may not be proxified for entity {1}. If they are explicitly implemented and access directly some internal state of the entity, unexpected outcome may occur.".

Due to the way NHibernate's proxies work, it could even be considered that trying to proxify entities implementing some internal interfaces is an error. But since the old proxies were not detecting the case and were letting silently these members unproxified, it would be a breaking change doable only for 6.0.

(It is an error because if some proxy instance is given back to some methods of the assembly holding the internal interfaces, it may call those methods. If they access directly internal or private state, they will then access the inherited proxy state instead of the delegated entity state handled by the proxy.)

Copy link
Member

@fredericDelaporte fredericDelaporte Apr 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the error case would also require, for actually being an error, to have these methods explicitly implemented. So unless this additional condition can be reliably detected, it should not be rejected as an error.

(Proposed warning message updated.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need any warnings here.

var typeBuilder = moduleBuilder.DefineType(typeName, typeAttributes, parentType, interfaces.ToArray());

var lazyInitializerField = typeBuilder.DefineField("__lazyInitializer", LazyInitializerType, FieldAttributes.Private);
Expand Down