Skip to content

Commit e26c26c

Browse files
author
Sébastien Geiser
committed
Refactoring
1 parent ddf0955 commit e26c26c

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ protected enum TryBlockEvaluatedState
382382
{ "new", (self, args) =>
383383
{
384384
List<object> cArgs = args.ConvertAll(self.Evaluate);
385-
return Activator.CreateInstance((cArgs[0] as ClassOrEnumType).Type, cArgs.Skip(1).ToArray());
385+
return cArgs[0] is ClassOrEnumType classOrEnumType ? Activator.CreateInstance(classOrEnumType.Type, cArgs.Skip(1).ToArray()) : null;
386386
}
387387
},
388388
{ "Round", (self, args) =>
@@ -433,7 +433,7 @@ protected enum TryBlockEvaluatedState
433433
/// Default : false
434434
/// the cache is the static Dictionary TypesResolutionCaching (so it is shared by all instances of ExpressionEvaluator that have CacheTypesResolutions enabled)
435435
/// </summary>
436-
public bool CacheTypesResolutions { get; set; } = false;
436+
public bool CacheTypesResolutions { get; set; }
437437

438438
/// <summary>
439439
/// A shared cache for types resolution.
@@ -1777,17 +1777,20 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
17771777
MethodInfo methodInfo = GetRealMethod(ref objType, ref obj, varFuncName, flag, oArgs, genericsTypes);
17781778

17791779
// if not found check if obj is an expandoObject or similar
1780-
if (obj is IDynamicMetaObjectProvider && obj is IDictionary<string, object> dictionaryObject && (dictionaryObject[varFuncName] is InternalDelegate || dictionaryObject[varFuncName] is Delegate))
1780+
if (obj is IDynamicMetaObjectProvider
1781+
&& obj is IDictionary<string, object> dictionaryObject
1782+
&& (dictionaryObject[varFuncName] is InternalDelegate || dictionaryObject[varFuncName] is Delegate))
17811783
{
17821784
if (dictionaryObject[varFuncName] is InternalDelegate internalDelegate)
17831785
stack.Push(internalDelegate(oArgs.ToArray()));
1784-
else
1785-
stack.Push((dictionaryObject[varFuncName] as Delegate).DynamicInvoke(oArgs.ToArray()));
1786+
else if(dictionaryObject[varFuncName] is Delegate del)
1787+
stack.Push(del.DynamicInvoke(oArgs.ToArray()));
17861788
}
17871789
else if (objType.GetProperty(varFuncName, InstanceBindingFlag) is PropertyInfo instancePropertyInfo
1788-
&& (instancePropertyInfo.PropertyType.IsSubclassOf(typeof(Delegate)) || instancePropertyInfo.PropertyType == typeof(Delegate)))
1790+
&& (instancePropertyInfo.PropertyType.IsSubclassOf(typeof(Delegate)) || instancePropertyInfo.PropertyType == typeof(Delegate))
1791+
&& instancePropertyInfo.GetValue(obj) is Delegate del)
17891792
{
1790-
stack.Push((instancePropertyInfo.GetValue(obj) as Delegate).DynamicInvoke(oArgs.ToArray()));
1793+
stack.Push(del.DynamicInvoke(oArgs.ToArray()));
17911794
}
17921795
else
17931796
{
@@ -1813,9 +1816,10 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
18131816
stack.Push(methodInfo.Invoke(isExtention ? null : obj, oArgs.ToArray()));
18141817
}
18151818
else if (objType.GetProperty(varFuncName, StaticBindingFlag) is PropertyInfo staticPropertyInfo
1816-
&& (staticPropertyInfo.PropertyType.IsSubclassOf(typeof(Delegate)) || staticPropertyInfo.PropertyType == typeof(Delegate)))
1819+
&& (staticPropertyInfo.PropertyType.IsSubclassOf(typeof(Delegate)) || staticPropertyInfo.PropertyType == typeof(Delegate))
1820+
&& staticPropertyInfo.GetValue(obj) is Delegate del2)
18171821
{
1818-
stack.Push((staticPropertyInfo.GetValue(obj) as Delegate).DynamicInvoke(oArgs.ToArray()));
1822+
stack.Push(del2.DynamicInvoke(oArgs.ToArray()));
18191823
}
18201824
else
18211825
{

0 commit comments

Comments
 (0)