5
5
using System . Linq . Expressions ;
6
6
using System . Reflection ;
7
7
using NHibernate . Linq . Visitors ;
8
- using Remotion . Linq . Utilities ;
9
8
10
9
namespace NHibernate . Linq
11
10
{
@@ -16,7 +15,7 @@ public abstract class Assignments
16
15
}
17
16
18
17
/// <summary>
19
- /// Class to hold assigments used in updates and inserts
18
+ /// Class to hold assignments used in updates and inserts
20
19
/// </summary>
21
20
/// <typeparam name="TInput">The type of the input.</typeparam>
22
21
/// <typeparam name="TOutput">The type of the output.</typeparam>
@@ -33,14 +32,13 @@ public class Assignments<TInput, TOutput> : Assignments
33
32
/// <returns></returns>
34
33
public Assignments < TInput , TOutput > Set < TProp > ( Expression < Func < TOutput , TProp > > property , Expression < Func < TInput , TProp > > expression )
35
34
{
36
- ArgumentUtility . CheckNotNull ( "property" , property ) ;
37
- ArgumentUtility . CheckNotNull ( "expression" , expression ) ;
38
- var member = property . Body as MemberExpression ;
35
+ if ( property == null )
36
+ throw new ArgumentNullException ( nameof ( property ) ) ;
37
+ if ( expression == null )
38
+ throw new ArgumentNullException ( nameof ( expression ) ) ;
39
39
var param = property . Parameters . Single ( ) ;
40
- if ( member == null )
41
- {
42
- throw new ArgumentException ( "The property expression must refer to a property of " + param . Name + "(" + param . Type . Name + ")" , "property" ) ;
43
- }
40
+ var member = property . Body as MemberExpression ??
41
+ throw new ArgumentException ( "The property expression must refer to a property of " + param . Name + "(" + param . Type . Name + ")" , nameof ( property ) ) ;
44
42
_sets . Add ( new Assignment ( member . GetMemberPath ( ) , expression ) ) ;
45
43
return this ;
46
44
}
@@ -54,13 +52,11 @@ public Assignments<TInput, TOutput> Set<TProp>(Expression<Func<TOutput, TProp>>
54
52
/// <returns></returns>
55
53
public Assignments < TInput , TOutput > Set < TProp > ( Expression < Func < TOutput , TProp > > property , TProp value )
56
54
{
57
- ArgumentUtility . CheckNotNull ( " property" , property ) ;
58
- var member = property . Body as MemberExpression ;
55
+ if ( property == null )
56
+ throw new ArgumentNullException ( nameof ( property ) ) ;
59
57
var param = property . Parameters . Single ( ) ;
60
- if ( member == null )
61
- {
62
- throw new ArgumentException ( "The property expression must refer to a property of " + param . Name + "(" + param . Type . Name + ")" , "property" ) ;
63
- }
58
+ var member = property . Body as MemberExpression ??
59
+ throw new ArgumentException ( "The property expression must refer to a property of " + param . Name + "(" + param . Type . Name + ")" , nameof ( property ) ) ;
64
60
_sets . Add ( new Assignment ( member . GetMemberPath ( ) , Expression . Constant ( value , typeof ( TProp ) ) ) ) ;
65
61
return this ;
66
62
}
@@ -76,8 +72,7 @@ public LambdaExpression ConvertToDictionaryExpression()
76
72
foreach ( var set in _sets )
77
73
{
78
74
var setter = set . Expression ;
79
- var setterLambda = setter as LambdaExpression ;
80
- if ( setterLambda != null )
75
+ if ( setter is LambdaExpression setterLambda )
81
76
{
82
77
setter = setterLambda . Body . Replace ( setterLambda . Parameters . First ( ) , param ) ;
83
78
}
@@ -108,14 +103,11 @@ public LambdaExpression ConvertToDictionaryExpression()
108
103
109
104
public static Assignments < TInput , TOutput > FromExpression ( Expression < Func < TInput , TOutput > > expression )
110
105
{
111
- ArgumentUtility . CheckNotNull ( "expression" , expression ) ;
106
+ if ( expression == null )
107
+ throw new ArgumentNullException ( nameof ( expression ) ) ;
112
108
var instance = new Assignments < TInput , TOutput > ( ) ;
113
- var memberInitExpression = expression . Body as MemberInitExpression ;
114
-
115
- if ( memberInitExpression == null )
116
- {
109
+ var memberInitExpression = expression . Body as MemberInitExpression ??
117
110
throw new ArgumentException ( "The expression must be member initialization, e.g. x => new Dog{Name = x.Name,Age = x.Age + 5}" ) ;
118
- }
119
111
120
112
AddSetsFromBindings ( memberInitExpression . Bindings , instance , "" , expression . Parameters ) ;
121
113
@@ -139,8 +131,8 @@ private static void AddSetsFromBindings(IEnumerable<MemberBinding> bindings, Ass
139
131
140
132
private static void AddSetsFromAssignment ( MemberAssignment assignment , Assignments < TInput , TOutput > instance , string path , ReadOnlyCollection < ParameterExpression > parameters )
141
133
{
142
- var memberInit = assignment . Expression as MemberInitExpression ; // {Property=new Instance{SubProperty="Value"}}
143
- if ( memberInit != null )
134
+ // {Property=new Instance{SubProperty="Value"}}
135
+ if ( assignment . Expression is MemberInitExpression memberInit )
144
136
{
145
137
AddSetsFromBindings ( memberInit . Bindings , instance , path , parameters ) ;
146
138
}
0 commit comments