/// <summary> /// Determines maximum using a conversion to normalize type. /// </summary> public static Computer CreateMaxDoubleComputer(ExprEvaluator[] childNodes) { var typeCaster = CastHelper.GetCastConverter <Double>(); return(delegate(EventBean[] eventsPerStream, bool isNewData, ExprEvaluatorContext exprEvaluatorContext) { Object valueResult = null; Double typedResult = Double.MinValue; for (int ii = 0; ii < childNodes.Length; ii++) { var valueChild = childNodes[ii].Evaluate(new EvaluateParams(eventsPerStream, isNewData, exprEvaluatorContext)); if (valueChild == null) { return null; } var typedChild = typeCaster(valueChild); if (typedChild > typedResult) { valueResult = valueChild; typedResult = typedChild; } } return valueResult; }); }
/// <summary> /// Determines minimum using a conversion to normalize type. /// </summary> public static Computer CreateMinBigIntComputer(ExprEvaluator[] childNodes) { var typeCaster = CastHelper.GetCastConverter <BigInteger>(); return(delegate(EventBean[] eventsPerStream, bool isNewData, ExprEvaluatorContext exprEvaluatorContext) { Object valueResult = null; BigInteger?typedResult = null; for (int ii = 0; ii < childNodes.Length; ii++) { var valueChild = childNodes[ii].Evaluate(new EvaluateParams(eventsPerStream, isNewData, exprEvaluatorContext)); if (valueChild == null) { return null; } var typedChild = typeCaster.Invoke(valueChild); if ((typedResult == null) || (typedChild < typedResult.Value)) { valueResult = valueChild; typedResult = typedChild; } } return valueResult; }); }
/// <summary> /// Checks the property type for array semantics. /// </summary> /// <param name="attributeTypeName">Name of the attribute type.</param> /// <param name="propertyInfo">The property info.</param> /// <param name="propertyName">Name of the property.</param> /// <param name="propertyValue">The property value.</param> /// <returns></returns> private static object CheckArray(string attributeTypeName, MagicPropertyInfo propertyInfo, string propertyName, object propertyValue) { if (propertyValue != null) { var actualElementType = propertyValue.GetType().GetElementType(); var expectElementType = propertyInfo.PropertyType.GetElementType(); // Did we actually receive an array as source? if (actualElementType == null) { throw new AttributeException( "Annotation '" + attributeTypeName + "' requires a " + propertyInfo.PropertyType.FullName + "-typed value for attribute '" + propertyName + "' but received a " + propertyValue.GetType().FullName + "-typed value"); } var array = (Array)propertyValue; var length = array.Length; for (var ii = 0; ii < length; ii++) { if (array.GetValue(ii) == null) { throw new AttributeException( "Annotation '" + attributeTypeName + "' requires a " + "non-null value for array elements for attribute '" + propertyName + "'"); } } if (!Equals(actualElementType, expectElementType)) { var typeCaster = CastHelper.GetCastConverter(expectElementType); var expectedArray = Array.CreateInstance(expectElementType, length); for (var ii = 0; ii < length; ii++) { var oldValue = array.GetValue(ii); var newValue = typeCaster.Invoke(oldValue); if ((newValue == null) && (oldValue != null)) { throw new AttributeException( "Annotation '" + attributeTypeName + "' requires a " + expectElementType.FullName + "-typed value for array elements for attribute '" + propertyName + "' but received a " + oldValue.GetType().FullName + "-typed value"); } expectedArray.SetValue(newValue, ii); } propertyValue = expectedArray; } } return(propertyValue); }
/// <summary> /// Checks the property type for a type mismatch. /// </summary> /// <param name="attributeTypeName">Name of the attribute type.</param> /// <param name="magicPropertyType">Type of the magic property.</param> /// <param name="propertyName">Name of the property.</param> /// <param name="propertyValue">The property value.</param> private static object CheckTypeMismatch(string attributeTypeName, Type magicPropertyType, string propertyName, object propertyValue) { var isTypeMismatch = false; if (magicPropertyType.IsValueType) { if (propertyValue == null) { isTypeMismatch = true; } else if (magicPropertyType.IsEnum && (propertyValue is string)) { return(Enum.Parse(magicPropertyType, (string)propertyValue, true)); } else if (!propertyValue.GetType().IsAssignableFrom(magicPropertyType)) { var typeCaster = CastHelper.GetCastConverter(magicPropertyType); var newValue = typeCaster.Invoke(propertyValue); if (newValue != null) { propertyValue = newValue; } else { isTypeMismatch = true; } } } else if (propertyValue != null) { if (!propertyValue.GetType().IsAssignableFrom(magicPropertyType)) { isTypeMismatch = true; } } if (isTypeMismatch) { var propertyValueText = propertyValue != null?propertyValue.GetType().FullName : "null"; throw new AttributeException( "Annotation '" + attributeTypeName + "' requires a " + magicPropertyType.Name + "-typed value for attribute '" + propertyName + "' but received " + "a " + propertyValueText + "-typed value"); } return(propertyValue); }
public static T DecodePrimitive <T>(this PrimitiveSchema schema, JToken value) { var jvalue = value as JValue; if (jvalue != null) { var underlying = jvalue.Value; if (underlying is T) { return((T)underlying); } var castConverter = CastHelper.GetCastConverter <T>(); if (castConverter != null) { return(castConverter(underlying)); } } throw new ArgumentException("invalid value type: " + value.GetType().FullName); }
/// <summary> /// Static cast method used in assignment. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value">The value.</param> /// <returns></returns> public static T CastTo <T>(Object value) { if (value is T) { return((T)value); } // Arrays need to be converted by looking at the internal elements // within the array. Since value is more than likely going to be // an array of System.Object, the conversion is basically a recursive // call to this method. if (typeof(T).IsArray) { var valueArray = value as Object[]; if (valueArray == null) { return(default(T)); // null } var subType = typeof(T).GetElementType(); var subCast = typeof(MagicPropertyInfo) .GetMethod("CastTo") .MakeGenericMethod(subType); var returnArray = Array.CreateInstance(subType, valueArray.Length); for (int ii = 0; ii < valueArray.Length; ii++) { returnArray.SetValue(subCast.Invoke(null, new[] { valueArray[ii] }), ii); } return((T)((Object)returnArray)); } var genericTypeCaster = CastHelper.GetCastConverter <T>(); return(genericTypeCaster(value)); }