@@ -451,8 +451,22 @@ private enum TryBlockEvaluatedState
451451
452452 #region Caching
453453
454+ /// <summary>
455+ /// if set to <c>true</c> use a cache for types that were resolved to resolve faster next time.
456+ /// if set to <c>false</c> the cach of types resolution is not use for this instance of ExpressionEvaluator.
457+ /// Default : false
458+ /// the cache is the static Dictionary TypesResolutionCaching (so it is shared by all instances of ExpressionEvaluator that have CacheTypesResolutions enabled)
459+ /// </summary>
460+ public bool CacheTypesResolutions { get ; set ; } = false ;
461+
462+ /// <summary>
463+ /// A shared cache for types resolution.
464+ /// </summary>
454465 public static IDictionary < string , Type > TypesResolutionCaching { get ; set ; } = new Dictionary < string , Type > ( ) ;
455466
467+ /// <summary>
468+ /// Clear all ExpressionEvaluator caches
469+ /// </summary>
456470 public static void ClearAllCaches ( )
457471 {
458472 TypesResolutionCaching . Clear ( ) ;
@@ -2980,20 +2994,29 @@ private bool DefaultFunctions(string name, List<string> args, out object result)
29802994 private Type GetTypeByFriendlyName ( string typeName , string genericTypes = "" , bool throwExceptionIfNotFound = false )
29812995 {
29822996 Type result = null ;
2997+ string formatedGenericTypes = string . Empty ;
2998+ bool isCached = false ;
29832999 try
29843000 {
29853001 typeName = typeName . Replace ( " " , "" ) . Replace ( "\t " , "" ) . Replace ( "\r " , "" ) . Replace ( "\n " , "" ) ;
29863002 genericTypes = genericTypes . Replace ( " " , "" ) . Replace ( "\t " , "" ) . Replace ( "\r " , "" ) . Replace ( "\n " , "" ) ;
29873003
2988- string formatedGenericTypes = string . Empty ;
2989-
2990- if ( ! genericTypes . Equals ( string . Empty ) )
3004+ if ( CacheTypesResolutions && ( TypesResolutionCaching ? . ContainsKey ( typeName + genericTypes ) ?? false ) )
29913005 {
2992- Type [ ] types = GetConcreteTypes ( genericTypes ) ;
2993- formatedGenericTypes = $ "` { types . Length } [ { string . Join ( ", " , types . Select ( type => type . FullName ) ) } ]" ;
3006+ result = TypesResolutionCaching [ typeName + genericTypes ] ;
3007+ isCached = true ;
29943008 }
29953009
2996- result = Type . GetType ( typeName + formatedGenericTypes , false , ! OptionCaseSensitiveEvaluationActive ) ;
3010+ if ( result == null )
3011+ {
3012+ if ( ! genericTypes . Equals ( string . Empty ) )
3013+ {
3014+ Type [ ] types = GetConcreteTypes ( genericTypes ) ;
3015+ formatedGenericTypes = $ "`{ types . Length } [{ string . Join ( ", " , types . Select ( type => type . FullName ) ) } ]";
3016+ }
3017+
3018+ result = Type . GetType ( typeName + formatedGenericTypes , false , ! OptionCaseSensitiveEvaluationActive ) ;
3019+ }
29973020
29983021 if ( result == null )
29993022 {
@@ -3035,6 +3058,9 @@ private Type GetTypeByFriendlyName(string typeName, string genericTypes = "", bo
30353058 if ( result == null && throwExceptionIfNotFound )
30363059 throw new ExpressionEvaluatorSyntaxErrorException ( $ "Type or class { typeName } { genericTypes } is unknown") ;
30373060
3061+ if ( CacheTypesResolutions && ( result != null ) && ! isCached )
3062+ TypesResolutionCaching [ typeName + genericTypes ] = result ;
3063+
30383064 return result ;
30393065 }
30403066
0 commit comments