Skip to content

Commit 88bdad0

Browse files
author
Mark Perry
committed
NH3500 - Unwrap TIE errors for classes with lazy properties
1 parent a6c78c3 commit 88bdad0

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using NHibernate.Intercept;
6+
using NHibernate.Proxy;
7+
using NHibernate.Test.DynamicProxyTests;
8+
using NUnit.Framework;
9+
10+
namespace NHibernate.Test.NHSpecificTest.NH3500
11+
{
12+
[Serializable]
13+
public class MyClass
14+
{
15+
public virtual int Id { get; set; }
16+
17+
public virtual void ThrowError()
18+
{
19+
throw new Exception("test");
20+
}
21+
}
22+
23+
[TestFixture]
24+
public class Fixture
25+
{
26+
[Test]
27+
public void DefaultDynamicLazyFieldInterceptorUnWrapsTIEExceptions()
28+
{
29+
var pf = new DefaultProxyFactory();
30+
var propertyInfo = typeof(MyClass).GetProperty("Id");
31+
pf.PostInstantiate("MyClass", typeof(MyClass), new HashSet<System.Type>(), propertyInfo.GetGetMethod(), propertyInfo.GetSetMethod(), null);
32+
var myClassProxied = (MyClass)pf.GetFieldInterceptionProxy(new MyClass());
33+
Assert.Throws<Exception>(() => myClassProxied.ThrowError(), "test");
34+
}
35+
}
36+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@
731731
<Compile Include="NHSpecificTest\NH2931\Models.cs" />
732732
<Compile Include="NHSpecificTest\NH3046\Fixture.cs" />
733733
<Compile Include="NHSpecificTest\NH3046\Model.cs" />
734+
<Compile Include="NHSpecificTest\NH3500\Fixture.cs" />
734735
<Compile Include="NHSpecificTest\NH3518\ClassWithXmlMember.cs" />
735736
<Compile Include="NHSpecificTest\NH3518\XmlColumnTest.cs" />
736737
<Compile Include="NHSpecificTest\NH3609\MappingEntity.cs" />
@@ -3754,7 +3755,6 @@
37543755
<Folder Include="Properties\" />
37553756
</ItemGroup>
37563757
<ItemGroup>
3757-
37583758
</ItemGroup>
37593759
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
37603760
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

src/NHibernate/Intercept/DefaultDynamicLazyFieldInterceptor.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Reflection;
23
using NHibernate.Proxy.DynamicProxy;
34
using NHibernate.Util;
45

@@ -50,7 +51,16 @@ public object Intercept(InvocationInfo info)
5051
}
5152
}
5253

53-
return info.InvokeMethodOnTarget();
54+
object returnValue;
55+
try
56+
{
57+
returnValue = info.InvokeMethodOnTarget();
58+
}
59+
catch (TargetInvocationException ex)
60+
{
61+
throw ReflectHelper.UnwrapTargetInvocationException(ex);
62+
}
63+
return returnValue;
5464
}
5565
}
5666
}

0 commit comments

Comments
 (0)