Skip to content

Commit 18ac21e

Browse files
committed
NH-3795 - Use Regex to identify compiler generated members in FindMemberExpression
1 parent 139106a commit 18ac21e

File tree

2 files changed

+77
-15
lines changed

2 files changed

+77
-15
lines changed

src/NHibernate.Test/NHSpecificTest/NH3795/Fixture.cs

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,14 @@ public class Fixture : TestCase
1212
{
1313
protected Child childAliasField = null;
1414
protected A aAliasField = null;
15-
15+
1616
protected override IList Mappings
1717
{
18-
get
19-
{
20-
return new[] { "ParentChild.hbm.xml", "ABC.hbm.xml" };
21-
}
18+
get { return new[] {"ParentChild.hbm.xml", "ABC.hbm.xml"}; }
2219
}
23-
24-
20+
2521
[Test]
26-
public void TestAliasInQueryOver()
22+
public void TestFieldAliasInQueryOver()
2723
{
2824
using (var s = sessions.OpenSession())
2925
{
@@ -34,9 +30,9 @@ public void TestAliasInQueryOver()
3430
.List();
3531
}
3632
}
37-
33+
3834
[Test]
39-
public void TestAliasInQueryOverWithConversion()
35+
public void TestFieldAliasInQueryOverWithConversion()
4036
{
4137
using (var s = sessions.OpenSession())
4238
{
@@ -47,9 +43,9 @@ public void TestAliasInQueryOverWithConversion()
4743
.List();
4844
}
4945
}
50-
46+
5147
[Test]
52-
public void TestAliasInJoinAlias()
48+
public void TestFieldAliasInJoinAlias()
5349
{
5450
using (var s = sessions.OpenSession())
5551
{
@@ -61,9 +57,9 @@ public void TestAliasInJoinAlias()
6157
.List();
6258
}
6359
}
64-
60+
6561
[Test]
66-
public void TestAliasInJoinQueryOver()
62+
public void TestFieldAliasInJoinQueryOver()
6763
{
6864
using (var s = sessions.OpenSession())
6965
{
@@ -75,5 +71,67 @@ public void TestAliasInJoinQueryOver()
7571
.List();
7672
}
7773
}
74+
75+
[Test]
76+
public void TestAliasInQueryOver()
77+
{
78+
Child childAlias = null;
79+
A aAlias = null;
80+
using (var s = sessions.OpenSession())
81+
{
82+
A rowalias = null;
83+
s.QueryOver(() => aAlias)
84+
.SelectList(list => list
85+
.Select(() => aAlias.Id).WithAlias(() => rowalias.Id))
86+
.List();
87+
}
88+
}
89+
90+
[Test]
91+
public void TestAliasInQueryOverWithConversion()
92+
{
93+
Child childAlias = null;
94+
A aAlias = null;
95+
using (var s = sessions.OpenSession())
96+
{
97+
B rowalias = null;
98+
s.QueryOver(() => aAlias)
99+
.SelectList(list => list
100+
.Select(() => ((B) aAlias).Count).WithAlias(() => rowalias.Count))
101+
.List();
102+
}
103+
}
104+
105+
[Test]
106+
public void TestAliasInJoinAlias()
107+
{
108+
Child childAlias = null;
109+
A aAlias = null;
110+
using (var s = sessions.OpenSession())
111+
{
112+
Child rowalias = null;
113+
s.QueryOver<Parent>()
114+
.JoinAlias(p => p.Child, () => childAlias)
115+
.SelectList(list => list
116+
.Select(() => childAlias.Id).WithAlias(() => rowalias.Id))
117+
.List();
118+
}
119+
}
120+
121+
[Test]
122+
public void TestAliasInJoinQueryOver()
123+
{
124+
Child childAlias = null;
125+
A aAlias = null;
126+
using (var s = sessions.OpenSession())
127+
{
128+
Child rowalias = null;
129+
s.QueryOver<Parent>()
130+
.JoinQueryOver(p => p.Child, () => childAlias)
131+
.SelectList(list => list
132+
.Select(() => childAlias.Id).WithAlias(() => rowalias.Id))
133+
.List();
134+
}
135+
}
78136
}
79137
}

src/NHibernate/Impl/ExpressionProcessor.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq.Expressions;
66
using System.Reflection;
77
using System.Runtime.CompilerServices;
8+
using System.Text.RegularExpressions;
89
using NHibernate.Criterion;
910
using NHibernate.Util;
1011
using Expression = System.Linq.Expressions.Expression;
@@ -290,13 +291,16 @@ public static ProjectionInfo FindMemberProjection(Expression expression)
290291
return ProjectionInfo.ForProperty(FindMemberExpression(expression));
291292
}
292293

294+
//http://stackoverflow.com/a/2509524/259946
295+
private static readonly Regex GeneratedMemberNameRegex = new Regex(@"^(CS\$)?<\w*>[1-9a-s]__[a-zA-Z]+[0-9]*$", RegexOptions.Compiled | RegexOptions.Singleline);
296+
293297
private static bool IsCompilerGeneratedMemberExpressionOfCompilerGeneratedClass(Expression expression)
294298
{
295299
var memberExpression = expression as MemberExpression;
296300
if (memberExpression != null && memberExpression.Member.DeclaringType != null)
297301
{
298302
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?
303+
&& GeneratedMemberNameRegex.IsMatch(memberExpression.Member.Name);
300304
}
301305

302306
return false;

0 commit comments

Comments
 (0)