Skip to content

Commit 139106a

Browse files
gerritsenhazzik
authored andcommitted
NH-3795 - Ignore compiler generated members of compiler generated classes in FindMemberExpression
1 parent 5c739ad commit 139106a

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System.Collections;
2+
using NHibernate.DomainModel;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.NHSpecificTest.NH3795
6+
{
7+
/// <summary>
8+
/// Tests in this class only failed when the code was build with the Roslyn compiler which is included in Visual Studio 2015
9+
/// </summary>
10+
[TestFixture]
11+
public class Fixture : TestCase
12+
{
13+
protected Child childAliasField = null;
14+
protected A aAliasField = null;
15+
16+
protected override IList Mappings
17+
{
18+
get
19+
{
20+
return new[] { "ParentChild.hbm.xml", "ABC.hbm.xml" };
21+
}
22+
}
23+
24+
25+
[Test]
26+
public void TestAliasInQueryOver()
27+
{
28+
using (var s = sessions.OpenSession())
29+
{
30+
A rowalias = null;
31+
s.QueryOver(() => aAliasField)
32+
.SelectList(list => list
33+
.Select(() => aAliasField.Id).WithAlias(() => rowalias.Id))
34+
.List();
35+
}
36+
}
37+
38+
[Test]
39+
public void TestAliasInQueryOverWithConversion()
40+
{
41+
using (var s = sessions.OpenSession())
42+
{
43+
B rowalias = null;
44+
s.QueryOver(() => aAliasField)
45+
.SelectList(list => list
46+
.Select(() => ((B)aAliasField).Count).WithAlias(() => rowalias.Count))
47+
.List();
48+
}
49+
}
50+
51+
[Test]
52+
public void TestAliasInJoinAlias()
53+
{
54+
using (var s = sessions.OpenSession())
55+
{
56+
Child rowalias = null;
57+
s.QueryOver<Parent>()
58+
.JoinAlias(p => p.Child, () => childAliasField)
59+
.SelectList(list => list
60+
.Select(() => childAliasField.Id).WithAlias(() => rowalias.Id))
61+
.List();
62+
}
63+
}
64+
65+
[Test]
66+
public void TestAliasInJoinQueryOver()
67+
{
68+
using (var s = sessions.OpenSession())
69+
{
70+
Child rowalias = null;
71+
s.QueryOver<Parent>()
72+
.JoinQueryOver(p => p.Child, () => childAliasField)
73+
.SelectList(list => list
74+
.Select(() => childAliasField.Id).WithAlias(() => rowalias.Id))
75+
.List();
76+
}
77+
}
78+
}
79+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@
810810
<Compile Include="NHSpecificTest\NH3202\Fixture.cs" />
811811
<Compile Include="NHSpecificTest\NH3604\Entity.cs" />
812812
<Compile Include="NHSpecificTest\NH3604\FixtureByCode.cs" />
813+
<Compile Include="NHSpecificTest\NH3795\Fixture.cs" />
813814
<Compile Include="NHSpecificTest\NH646\Domain.cs" />
814815
<Compile Include="NHSpecificTest\NH646\Fixture.cs" />
815816
<Compile Include="NHSpecificTest\NH3234\Fixture.cs" />

src/NHibernate/Impl/ExpressionProcessor.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Linq;
55
using System.Linq.Expressions;
66
using System.Reflection;
7-
7+
using System.Runtime.CompilerServices;
88
using NHibernate.Criterion;
99
using NHibernate.Util;
1010
using Expression = System.Linq.Expressions.Expression;
@@ -290,6 +290,18 @@ public static ProjectionInfo FindMemberProjection(Expression expression)
290290
return ProjectionInfo.ForProperty(FindMemberExpression(expression));
291291
}
292292

293+
private static bool IsCompilerGeneratedMemberExpressionOfCompilerGeneratedClass(Expression expression)
294+
{
295+
var memberExpression = expression as MemberExpression;
296+
if (memberExpression != null && memberExpression.Member.DeclaringType != null)
297+
{
298+
return Attribute.GetCustomAttribute(memberExpression.Member.DeclaringType, typeof(CompilerGeneratedAttribute)) != null
299+
&& memberExpression.Member.Name.StartsWith("<"); // Is there another way to check for a compiler generated member?
300+
}
301+
302+
return false;
303+
}
304+
293305
/// <summary>
294306
/// Retrieves the name of the property from a member expression
295307
/// </summary>
@@ -311,6 +323,11 @@ public static string FindMemberExpression(Expression expression)
311323
return FindMemberExpression(memberExpression.Expression);
312324
}
313325

326+
if (IsCompilerGeneratedMemberExpressionOfCompilerGeneratedClass(memberExpression.Expression))
327+
{
328+
return memberExpression.Member.Name;
329+
}
330+
314331
return FindMemberExpression(memberExpression.Expression) + "." + memberExpression.Member.Name;
315332
}
316333
if (IsConversion(memberExpression.Expression.NodeType))

0 commit comments

Comments
 (0)