@@ -2117,19 +2117,20 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
2117
2117
else if ( ( Variables . TryGetValue ( varFuncName , out object cusVarValueToPush ) || varFuncMatch . Groups [ "assignationOperator" ] . Success )
2118
2118
&& ( cusVarValueToPush == null || ! TypesToBlock . Contains ( cusVarValueToPush . GetType ( ) ) ) )
2119
2119
{
2120
- StronglyTypedVariable stronglyTypedVariable ;
2121
-
2122
2120
if ( stack . Count == 1 && stack . Peek ( ) is ClassOrEnumType classOrEnum && varFuncMatch . Groups [ "assignationOperator" ] . Success )
2123
2121
{
2124
2122
stack . Pop ( ) ;
2125
2123
2126
- stronglyTypedVariable = new StronglyTypedVariable
2124
+ Variables [ varFuncName ] = new StronglyTypedVariable
2127
2125
{
2128
2126
Type = classOrEnum . Type ,
2129
2127
Value = ! varFuncMatch . Groups [ "assignationOperator" ] . Success && classOrEnum . Type . IsValueType ? Activator . CreateInstance ( classOrEnum . Type ) : null ,
2130
2128
} ;
2131
2129
}
2132
2130
2131
+ if ( cusVarValueToPush is StronglyTypedVariable typedVariable )
2132
+ cusVarValueToPush = typedVariable . Value ;
2133
+
2133
2134
stack . Push ( cusVarValueToPush ) ;
2134
2135
2135
2136
if ( OptionVariableAssignationActive )
@@ -2180,7 +2181,29 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
2180
2181
}
2181
2182
2182
2183
if ( assign )
2183
- Variables [ varFuncName ] = cusVarValueToPush ;
2184
+ {
2185
+ if ( Variables . ContainsKey ( varFuncName ) && Variables [ varFuncName ] is StronglyTypedVariable stronglyTypedVariable )
2186
+ {
2187
+ if ( cusVarValueToPush == null && ! stronglyTypedVariable . Type . IsValueType )
2188
+ {
2189
+ throw new ExpressionEvaluatorSyntaxErrorException ( $ "Can not cast null to { stronglyTypedVariable . Type } because it's not a nullable valueType") ;
2190
+ }
2191
+
2192
+ Type typeToAssign = cusVarValueToPush ? . GetType ( ) ;
2193
+ if ( typeToAssign == null || stronglyTypedVariable . Type . IsAssignableFrom ( typeToAssign ) )
2194
+ {
2195
+ stronglyTypedVariable . Value = cusVarValueToPush ;
2196
+ }
2197
+ else
2198
+ {
2199
+ throw new InvalidCastException ( $ "A object of type { typeToAssign } can not be cast implicitely in { stronglyTypedVariable . Type } ") ;
2200
+ }
2201
+ }
2202
+ else
2203
+ {
2204
+ Variables [ varFuncName ] = cusVarValueToPush ;
2205
+ }
2206
+ }
2184
2207
}
2185
2208
else if ( varFuncMatch . Groups [ "assignationOperator" ] . Success )
2186
2209
{
0 commit comments