/// <summary> /// Builds an array of signatures for math functions. /// </summary> /// <param name="methodName">The name of the method to call on the Math class.</param> /// <returns>The array of signatures for math functions.</returns> private static BuiltInFunctionSignature[] CreateMathFunctionSignatureArray(string methodName) { BuiltInFunctionSignature doubleSignature = BuiltInFunctionSignature.CreateFromStaticMethodCall( typeof(Math), methodName, EdmCoreModel.Instance.GetDouble(false), EdmCoreModel.Instance, EdmCoreModel.Instance.GetDouble(false)); BuiltInFunctionSignature nullableDoubleSignature = BuiltInFunctionSignature.CreateFromStaticMethodCall( typeof(Math), methodName, EdmCoreModel.Instance.GetDouble(false), EdmCoreModel.Instance, EdmCoreModel.Instance.GetDouble(true)); BuiltInFunctionSignature decimalSignature = BuiltInFunctionSignature.CreateFromStaticMethodCall( typeof(Math), methodName, EdmCoreModel.Instance.GetDecimal(false), EdmCoreModel.Instance, EdmCoreModel.Instance.GetDecimal(false)); BuiltInFunctionSignature nullableDecimalSignature = BuiltInFunctionSignature.CreateFromStaticMethodCall( typeof(Math), methodName, EdmCoreModel.Instance.GetDecimal(false), EdmCoreModel.Instance, EdmCoreModel.Instance.GetDecimal(true)); return(new BuiltInFunctionSignature[] { doubleSignature, decimalSignature, nullableDoubleSignature, nullableDecimalSignature }); }
/// <summary> /// Returns a list of signatures for a function name. /// </summary> /// <param name="name">The name of the function to look for.</param> /// <param name="signatures">The list of signatures available for the function name.</param> /// <returns>true if the function was found, or false otherwise.</returns> internal static bool TryGetBuiltInFunction(string name, out BuiltInFunctionSignature[] signatures) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(name != null, "name != null"); InitializeBuiltInFunctions(); return builtInFunctions.TryGetValue(name, out signatures); }
/// <summary> /// Builds an array of signatures for date time functions. /// </summary> /// <param name="propertyName">The name of the property on DateTime to access.</param> /// <returns>The array of signatures for a date time functions.</returns> private static BuiltInFunctionSignature[] CreateDateTimeFunctionSignatureArray(string propertyName) { BuiltInFunctionSignature signatureNonNullable = BuiltInFunctionSignature.CreateFromPropertyAccess( propertyName, EdmCoreModel.Instance.GetInt32(false), EdmCoreModel.Instance, EdmCoreModel.Instance.GetTemporal(EdmPrimitiveTypeKind.DateTime, false)); BuiltInFunctionSignature signatureNullable = BuiltInFunctionSignature.CreateFromPropertyAccess( propertyName, EdmCoreModel.Instance.GetInt32(false), EdmCoreModel.Instance, EdmCoreModel.Instance.GetTemporal(EdmPrimitiveTypeKind.DateTime, true)); return(new BuiltInFunctionSignature[] { signatureNonNullable, signatureNullable }); }
/// <summary> /// Creates a signature for a function which is executed by accessing a property. /// </summary> /// <param name="propertyName">The name of the CLR property to access on the first argument.</param> /// <param name="returnType">The return type of the function.</param> /// <param name="model">The model containing annotations.</param> /// <param name="argumentTypes">The types of the arguments of the function. (must be exactly one).</param> /// <returns>Newly created function signature.</returns> /// <remarks>Note that arguments are always treated as non-nullable for the CLR method search, even if the ones specified are nullable.</remarks> internal static BuiltInFunctionSignature CreateFromPropertyAccess(string propertyName, IEdmPrimitiveTypeReference returnType, IEdmModel model, params IEdmPrimitiveTypeReference[] argumentTypes) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(model != null, "model != null"); Debug.Assert(argumentTypes != null, "argumentTypes != null"); Debug.Assert(argumentTypes.Length == 1, "Property access built-in function is only allowed to have one argument."); PropertyInfo property = TypeUtils.GetNonNullableType(argumentTypes[0].GetInstanceType(model)).GetProperty( propertyName, BindingFlags.Public | BindingFlags.Instance); Debug.Assert(property != null, "Could not find the specified property on the target type."); BuiltInFunctionSignature signature = new BuiltInFunctionSignature(returnType, argumentTypes); signature.memberInfo = property; signature.buildExpression = signature.BuildPropertyAccessExpression; return(signature); }
/// <summary> /// Creates a signature for a function which is executed by calling an instance method. /// </summary> /// <param name="methodName">The name of the CLR method to call.</param> /// <param name="returnType">The return type of the function.</param> /// <param name="model">The model containing annotations.</param> /// <param name="argumentTypes">The types of the arguments of the function.</param> /// <returns>Newly created function signature.</returns> /// <remarks>Note that arguments are always treated as non-nullable for the CLR method search, even if the ones specified are nullable.</remarks> internal static BuiltInFunctionSignature CreateFromInstanceMethodCall(string methodName, IEdmPrimitiveTypeReference returnType, IEdmModel model, params IEdmPrimitiveTypeReference[] argumentTypes) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(model != null, "model != null"); MethodInfo method = argumentTypes[0].GetInstanceType(model).GetMethod( methodName, BindingFlags.Public | BindingFlags.Instance, null, argumentTypes.Skip(1).Select(argumentType => TypeUtils.GetNonNullableType(argumentType.GetInstanceType(model))).ToArray(), null); Debug.Assert(method != null, "Could not find the specified method on the type of the first argument."); BuiltInFunctionSignature signature = new BuiltInFunctionSignature(returnType, argumentTypes); signature.memberInfo = method; signature.buildExpression = signature.BuildInstanceMethodCallExpression; return(signature); }
/// <summary> /// Creates all string functions. /// </summary> private static void CreateStringFunctions() { BuiltInFunctionSignature signature; BuiltInFunctionSignature[] signatures; // bool endswith(string, string) signature = BuiltInFunctionSignature.CreateFromInstanceMethodCall( "EndsWith", EdmCoreModel.Instance.GetBoolean(false), EdmCoreModel.Instance, EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance.GetString(true)); builtInFunctions.Add("endswith", new BuiltInFunctionSignature[] { signature }); // int indexof(string, string) signature = BuiltInFunctionSignature.CreateFromInstanceMethodCall( "IndexOf", EdmCoreModel.Instance.GetInt32(false), EdmCoreModel.Instance, EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance.GetString(true)); builtInFunctions.Add("indexof", new BuiltInFunctionSignature[] { signature }); // string replace(string, string, string) signature = BuiltInFunctionSignature.CreateFromInstanceMethodCall( "Replace", EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance, EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance.GetString(true)); builtInFunctions.Add("replace", new BuiltInFunctionSignature[] { signature }); // bool startswith(string, string) signature = BuiltInFunctionSignature.CreateFromInstanceMethodCall( "StartsWith", EdmCoreModel.Instance.GetBoolean(false), EdmCoreModel.Instance, EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance.GetString(true)); builtInFunctions.Add("startswith", new BuiltInFunctionSignature[] { signature }); // string tolower(string) signature = BuiltInFunctionSignature.CreateFromInstanceMethodCall( "ToLower", EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance, EdmCoreModel.Instance.GetString(true)); builtInFunctions.Add("tolower", new BuiltInFunctionSignature[] { signature }); // string toupper(string) signature = BuiltInFunctionSignature.CreateFromInstanceMethodCall( "ToUpper", EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance, EdmCoreModel.Instance.GetString(true)); builtInFunctions.Add("toupper", new BuiltInFunctionSignature[] { signature }); // string trim(string) signature = BuiltInFunctionSignature.CreateFromInstanceMethodCall( "Trim", EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance, EdmCoreModel.Instance.GetString(true)); builtInFunctions.Add("trim", new BuiltInFunctionSignature[] { signature }); signatures = new BuiltInFunctionSignature[] { // string substring(string, int) BuiltInFunctionSignature.CreateFromInstanceMethodCall( "Substring", EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance, EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance.GetInt32(false)), // string substring(string, int?) BuiltInFunctionSignature.CreateFromInstanceMethodCall( "Substring", EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance, EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance.GetInt32(true)), // string substring(string, int, int) BuiltInFunctionSignature.CreateFromInstanceMethodCall( "Substring", EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance, EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance.GetInt32(false), EdmCoreModel.Instance.GetInt32(false)), // string substring(string, int?, int) BuiltInFunctionSignature.CreateFromInstanceMethodCall( "Substring", EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance, EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance.GetInt32(true), EdmCoreModel.Instance.GetInt32(false)), // string substring(string, int, int?) BuiltInFunctionSignature.CreateFromInstanceMethodCall( "Substring", EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance, EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance.GetInt32(false), EdmCoreModel.Instance.GetInt32(true)), // string substring(string, int?, int?) BuiltInFunctionSignature.CreateFromInstanceMethodCall( "Substring", EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance, EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance.GetInt32(true), EdmCoreModel.Instance.GetInt32(true)) }; builtInFunctions.Add("substring", signatures); // bool substringof(string, string) signature = new BuiltInFunctionSignature( BuildSubstringOfExpression, EdmCoreModel.Instance.GetBoolean(false), EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance.GetString(true)); builtInFunctions.Add("substringof", new BuiltInFunctionSignature[] { signature }); // string concat(string, string) signature = BuiltInFunctionSignature.CreateFromStaticMethodCall( typeof(string), "Concat", EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance, EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance.GetString(true)); builtInFunctions.Add("concat", new BuiltInFunctionSignature[] { signature }); // int length(string) signature = BuiltInFunctionSignature.CreateFromPropertyAccess( "Length", EdmCoreModel.Instance.GetInt32(false), EdmCoreModel.Instance, EdmCoreModel.Instance.GetString(true)); builtInFunctions.Add("length", new BuiltInFunctionSignature[] { signature }); }
/// <summary> /// Creates a signature for a function which is executed by accessing a property. /// </summary> /// <param name="propertyName">The name of the CLR property to access on the first argument.</param> /// <param name="returnType">The return type of the function.</param> /// <param name="model">The model containing annotations.</param> /// <param name="argumentTypes">The types of the arguments of the function. (must be exactly one).</param> /// <returns>Newly created function signature.</returns> /// <remarks>Note that arguments are always treated as non-nullable for the CLR method search, even if the ones specified are nullable.</remarks> internal static BuiltInFunctionSignature CreateFromPropertyAccess(string propertyName, IEdmPrimitiveTypeReference returnType, IEdmModel model, params IEdmPrimitiveTypeReference[] argumentTypes) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(model != null, "model != null"); Debug.Assert(argumentTypes != null, "argumentTypes != null"); Debug.Assert(argumentTypes.Length == 1, "Property access built-in function is only allowed to have one argument."); PropertyInfo property = TypeUtils.GetNonNullableType(argumentTypes[0].GetInstanceType(model)).GetProperty( propertyName, BindingFlags.Public | BindingFlags.Instance); Debug.Assert(property != null, "Could not find the specified property on the target type."); BuiltInFunctionSignature signature = new BuiltInFunctionSignature(returnType, argumentTypes); signature.memberInfo = property; signature.buildExpression = signature.BuildPropertyAccessExpression; return signature; }
/// <summary> /// Creates a signature for a function which is executed by calling a static method. /// </summary> /// <param name="targetType">The type on which to invoke the method.</param> /// <param name="methodName">The name of the CLR method to call.</param> /// <param name="returnType">The return type of the function.</param> /// <param name="model">The model containing annotations.</param> /// <param name="argumentTypes">The types of the arguments of the function.</param> /// <returns>Newly created function signature.</returns> /// <remarks>Note that arguments are always treated as non-nullable for the CLR method search, even if the ones specified are nullable.</remarks> internal static BuiltInFunctionSignature CreateFromStaticMethodCall(Type targetType, string methodName, IEdmPrimitiveTypeReference returnType, IEdmModel model, params IEdmPrimitiveTypeReference[] argumentTypes) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(targetType != null, "targetType != null"); Debug.Assert(model != null, "model != null"); MethodInfo method = targetType.GetMethod( methodName, BindingFlags.Public | BindingFlags.Static, null, argumentTypes.Select(argumentType => TypeUtils.GetNonNullableType(argumentType.GetInstanceType(model))).ToArray(), null); Debug.Assert(method != null, "Could not find the specified method on the target type."); BuiltInFunctionSignature signature = new BuiltInFunctionSignature(returnType, argumentTypes); signature.memberInfo = method; signature.buildExpression = signature.BuildStaticMethodCallExpression; return signature; }