Skip to content

Commit d5c906e

Browse files
Use pattern matching to check and perform type conversion (#572)
+semver:patch
1 parent 6ec649f commit d5c906e

File tree

3 files changed

+8
-17
lines changed

3 files changed

+8
-17
lines changed

src/FluentNHibernate/Automapping/AutoMapping.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ IEnumerable<Member> IMappingProvider.GetIgnoredProperties()
3939
void IAutoClasslike.AlterModel(ClassMappingBase mapping)
4040
{
4141
mapping.MergeAttributes(attributes.Clone());
42-
43-
var classMapping = mapping as ClassMapping;
44-
if (classMapping != null)
42+
if (mapping is ClassMapping classMapping)
4543
{
4644
if (providers.Id != null)
4745
classMapping.Set(x => x.Id, Layer.Defaults, providers.Id.GetIdentityMapping());

src/FluentNHibernate/MappingModel/TypeReference.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public override bool Equals(object obj)
148148
return Equals((TypeReference)obj);
149149
if (obj is Type)
150150
return Equals((Type)obj);
151-
if (obj.GetType() == typeof(string))
151+
if (obj is string)
152152
return Equals((string)obj);
153153

154154
return false;
@@ -202,4 +202,4 @@ public Type GetUnderlyingSystemType()
202202
return !(original == other);
203203
}
204204
}
205-
}
205+
}

src/FluentNHibernate/Utils/ExpressionToSql.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ public static string Convert<T>(Expression<Func<T, bool>> expression)
3232
{
3333
if (expression.Body is BinaryExpression)
3434
return Convert<T>((BinaryExpression)expression.Body);
35-
var memberExpression = expression.Body as MemberExpression;
36-
if (memberExpression != null && memberExpression.Type == typeof(bool))
35+
if (expression.Body is MemberExpression memberExpression && memberExpression.Type == typeof(bool))
3736
return Convert(CreateExpression<T>(memberExpression)) + " = " + Convert(true);
38-
var unaryExpression = expression.Body as UnaryExpression;
39-
if (unaryExpression != null && unaryExpression.Type == typeof(bool) && unaryExpression.NodeType == ExpressionType.Not)
37+
if (expression.Body is UnaryExpression unaryExpression && unaryExpression.Type == typeof(bool) && unaryExpression.NodeType == ExpressionType.Not)
4038
return Convert(CreateExpression<T>(unaryExpression.Operand)) + " = " + Convert(false);
4139

4240
throw new InvalidOperationException("Unable to convert expression to SQL");
@@ -68,19 +66,14 @@ private static string Convert(MethodCallExpression body)
6866

6967
private static string Convert<T>(Expression<Func<T, object>> expression, UnaryExpression body)
7068
{
71-
var constant = body.Operand as ConstantExpression;
7269

73-
if (constant != null)
70+
if (body.Operand is ConstantExpression constant)
7471
return Convert(constant);
7572

76-
var member = body.Operand as MemberExpression;
77-
78-
if (member != null)
73+
if (body.Operand is MemberExpression member)
7974
return Convert(expression, member);
8075

81-
var unaryExpression = body.Operand as UnaryExpression;
82-
83-
if (unaryExpression != null && unaryExpression.NodeType == ExpressionType.Convert)
76+
if (body.Operand is UnaryExpression unaryExpression && unaryExpression.NodeType == ExpressionType.Convert)
8477
return Convert(expression, unaryExpression);
8578

8679
throw new InvalidOperationException("Unable to convert expression to SQL");

0 commit comments

Comments
 (0)