/// <summary> /// Gets or adds a shared expression (created by the <paramref name="expressionFactory"/> if it's not already cached) with the given name, type, optionally /// for the given <paramref name="requestingType"/>. /// </summary> /// <param name="type">The runtime type of the Expression.</param> /// <param name="name">The runtime name of the Expression - and also the name used to retrieve it later.</param> /// <param name="expressionFactory">The factory method to use to construct the shared expression from scratch, if it's not already cached.</param> /// <param name="requestingType">Optional - to avoid naming clashes with shared expressions created by other targets, you can pass a type here /// (usually the runtime type of your <see cref="ITarget"/> implementation).</param> /// <returns>Expression.</returns> public Expression GetOrAddSharedExpression(Type type, string name, Func <Type, string, Expression> expressionFactory, Type requestingType = null) { if (this._sharedExpressions == null) { return(ParentContext.GetOrAddSharedExpression(type, name, expressionFactory, requestingType)); } if (type == null) { throw new ArgumentNullException(nameof(type)); } if (expressionFactory == null) { throw new ArgumentNullException(nameof(expressionFactory)); } // if this is SharedExpressionKey key = new SharedExpressionKey(type, name, requestingType); if (!this._sharedExpressions.TryGetValue(key, out Expression toReturn)) { this._sharedExpressions[key] = toReturn = expressionFactory(type, name); } return(toReturn); }