/// <summary> /// Gets the value of this curry cache according to the parameter association /// </summary> /// <param name="association"></param> /// <returns></returns> public ExplainedValue GetValue(Dictionary <Actual, IValue> association) { ExplainedValue retVal = null; FunctionCache current = Curry; foreach (IValue val in OrderedParameters(association)) { FunctionCache next; if (current.TryGetValue(val, out next)) { current = next; } else { current = null; break; } } if (current != null) { retVal = current.Value; } return(retVal); }
public static object GetValue(PropertyInfo property, object instance) { var key = GetMethodCacheKey(property); if (FunctionCache.TryGetValue(key, out Func <object, object> a) == false) { a = MakeGetMethod(property.GetGetMethod(), property.PropertyType); FunctionCache[key] = a; } return(a(instance)); }
/// <summary> /// Returns the first element of a sequence, or a default value if no element is found. /// </summary> /// <param name="type">The <see cref="Type"/> to return.</param> /// <param name="value"> /// The <see cref="IEnumerable"/> to return the item from. /// </param> /// <returns> /// The <see cref="object"/> representing the item. /// </returns> public static object FirstOrDefault(Type type, IEnumerable value) { var key = GetMethodCacheKey(type); if (FunctionCache.TryGetValue(key, out Func <object, object> f) == false) { f = StaticMethodSingleParameter <object>(FirstOrDefaultMethod.MakeGenericMethod(type)); FunctionCache[key] = f; } return(f(Cast(type, value))); }
/// <summary> /// Returns an empty <see cref="IEnumerable{T}"/> that has the specified type argument. /// </summary> /// <param name="type"> /// The <see cref="Type"/> to assign to the type parameter of the returned /// generic <see cref="IEnumerable{T}"/>. /// </param> /// <returns> /// The <see cref="object"/> representing the empty enumerable. /// </returns> public static object Empty(Type type) { var key = GetMethodCacheKey(type); if (FunctionCache.TryGetValue(key, out Func <object, object> f) == false) { f = StaticMethod <object>(EmptyMethod.MakeGenericMethod(type)); FunctionCache[key] = f; } return(f(type)); }
/// <summary> /// Casts the elements of the given <see cref="IEnumerable"/> to the specified type. /// </summary> /// <param name="type">The <see cref="Type"/> to cast to.</param> /// <param name="value"> /// The <see cref="IEnumerable"/> to cast the items of. /// </param> /// <returns> /// The <see cref="object"/> representing the cast enumerable. /// </returns> public static object Cast(Type type, IEnumerable value) { var key = GetMethodCacheKey(type); Func <object, object> f; if (!FunctionCache.TryGetValue(key, out f)) { f = StaticMethodSingleParameter <object>(CastMethod.MakeGenericMethod(type)); FunctionCache[key] = f; } return(f(value)); }
/// <summary> /// Gets the value of the property on the given instance. /// </summary> /// <param name="property">The property to set.</param> /// <param name="instance">The current instance to return the property from.</param> /// <returns>The <see cref="object"/> value.</returns> public static object GetValue(PropertyInfo property, object instance) { var key = GetMethodCacheKey(property); Func <object, object> a; if (!FunctionCache.TryGetValue(key, out a)) { a = BuildGetAccessor(property.GetGetMethod()); FunctionCache[key] = a; } return(a(instance)); }
private FunctionSymbol?TryGetFunctionSymbol(string name) { // symbol comparison relies on object equality; use of this cache ensures that different symbols with the same name are not returned. // we also cache negative lookups (null) so that we don't slow down when looking up references to a missing symbol if (FunctionCache.TryGetValue(name, out var symbol)) { return(symbol); } // wildcard match (e.g. list*) var wildcardOverloads = FunctionWildcardOverloads.Where(fo => fo.WildcardRegex.IsMatch(name)); // create a new symbol for each unique name that matches the wildcard return(wildcardOverloads.Any() ? new FunctionSymbol(name, wildcardOverloads) : null); }
/// <summary> /// Sets the value according to the parameter association /// </summary> /// <param name="association"></param> /// <param name="value"></param> public void SetValue(Dictionary <Actual, IValue> association, IValue value, ExplanationPart explanation) { FunctionCache current = Curry; foreach (IValue val in OrderedParameters(association)) { FunctionCache next; if (!current.TryGetValue(val, out next)) { next = new FunctionCache(); current.Add(val, next); } current = next; } current.Value = new ExplainedValue(value, explanation); }