public void ClrTypeMappingPrimitiveTypeOverflowTest() { var edmModel = this.GetParserResult(ClrTypeMappingTestModelBuilder.PrimitiveTypeOverflowTest()); var annotations = edmModel.FindVocabularyAnnotations(edmModel.FindType("NS1.Person")); Func <string, IEdmIntegerConstantExpression> GetIntegerExpression = (termName) => { var valueAnnotation = annotations.Single(n => n.Term.Name == termName) as IEdmValueAnnotation; return((IEdmIntegerConstantExpression)valueAnnotation.Value); }; Func <string, IEdmFloatingConstantExpression> GetFloatExpression = (termName) => { var valueAnnotation = annotations.Single(n => n.Term.Name == termName) as IEdmValueAnnotation; return((IEdmFloatingConstantExpression)valueAnnotation.Value); }; Func <string, IEdmValue> GetEdmValue = (termName) => { var valueAnnotation = annotations.Single(n => n.Term.Name == termName) as IEdmValueAnnotation; var edmToClrEvaluator = new EdmToClrEvaluator(null); return(edmToClrEvaluator.Evaluate(valueAnnotation.Value)); }; var edmToClrConverter = new EdmToClrConverter(); Assert.AreEqual(edmToClrConverter.AsClrValue(GetFloatExpression("SingleValue"), typeof(Single)), float.PositiveInfinity, "It should return Infinit for Single when the value is greater than Single.MaxValue."); Assert.AreEqual(edmToClrConverter.AsClrValue(GetFloatExpression("NegativeSingleValue"), typeof(Single)), float.NegativeInfinity, "It should return Negative Infinit for Single when the value is less than Single.MinValue."); this.VerifyThrowsException(typeof(OverflowException), () => edmToClrConverter.AsClrValue(GetEdmValue("ByteValue"), typeof(byte))); this.VerifyThrowsException(typeof(OverflowException), () => edmToClrConverter.AsClrValue <byte>(GetEdmValue("ByteValue"))); this.VerifyThrowsException(typeof(OverflowException), () => new EdmToClrEvaluator(null).EvaluateToClrValue <byte>(GetIntegerExpression("ByteValue"))); this.VerifyThrowsException(typeof(OverflowException), () => edmToClrConverter.AsClrValue(GetEdmValue("SByteValue"), typeof(sbyte))); this.VerifyThrowsException(typeof(OverflowException), () => edmToClrConverter.AsClrValue <sbyte>(GetEdmValue("SByteValue"))); this.VerifyThrowsException(typeof(OverflowException), () => new EdmToClrEvaluator(null).EvaluateToClrValue <sbyte>(GetIntegerExpression("SByteValue"))); }
private T ConvertToClrObject <T>(IEdmValueAnnotation valueAnnotation) { var edmClrEvaluator = new EdmToClrEvaluator(this.operationDefinitions); var edmClrConverter = new EdmToClrConverter(); var edmValue = edmClrEvaluator.Evaluate(valueAnnotation.Value); var object1 = edmClrEvaluator.EvaluateToClrValue <T>(valueAnnotation.Value); var object2 = (T)edmClrConverter.AsClrValue(edmValue, typeof(T)); var object3 = edmClrEvaluator.EdmToClrConverter.AsClrValue <T>(edmValue); Assert.IsTrue(CompareObjects(object1, object2), "The results of EvaluateToClrValue and AsClrValue do not match."); Assert.IsTrue(CompareObjects(object2, object3), "The result of the generic version of AsClrValue does not match that of the non-generic version."); return(object1); }
public void ClrTypeMappingValueAnnotationEnumTest() { var edmModel = this.GetParserResult(ClrTypeMappingTestModelBuilder.ValueAnnotationEnumTest()); var annotations = edmModel.FindVocabularyAnnotations(edmModel.FindType("NS1.Person")); Func <string, IEdmIntegerConstantExpression> GetIntegerExpression = (termName) => { var valueAnnotation = annotations.Single(n => n.Term.Name == termName) as IEdmValueAnnotation; return((IEdmIntegerConstantExpression)valueAnnotation.Value); }; Func <string, IEdmValue> GetEdmValue = (termName) => { var valueAnnotation = annotations.Single(n => n.Term.Name == termName) as IEdmValueAnnotation; var edmToClrEvaluator = new EdmToClrEvaluator(null); return(edmToClrEvaluator.Evaluate(valueAnnotation.Value)); }; var edmToClrConverter = new EdmToClrConverter(); Assert.AreEqual(edmToClrConverter.AsClrValue(GetIntegerExpression("PersonValueAnnotation3"), typeof(EnumInt)), EnumInt.Member1, "It should return Infinit for Single when the value is greater than Single.MaxValue."); Assert.AreEqual(new EdmToClrEvaluator(null).EvaluateToClrValue <EnumInt>(GetIntegerExpression("PersonValueAnnotation3")), EnumInt.Member1, "It should return Infinit for Single when the value is greater than Single.MaxValue."); Assert.AreEqual(new EdmToClrEvaluator(null).EvaluateToClrValue <EnumInt>(GetIntegerExpression("PersonValueAnnotation4")), (EnumInt)(-2), "It should return Infinit for Single when the value is greater than Single.MaxValue."); #if !SILVERLIGHT this.ValidateClrObjectConverter(this.GetValueAnnotations(edmModel, edmModel.FindType("NS1.Person"), "PersonValueAnnotation1").Single(), new ClassWithEnum() { EnumInt = EnumInt.Member1, EnumByte = (EnumByte)10, EnumULong = EnumULong.Member2 }); #endif this.VerifyThrowsException(typeof(InvalidCastException), () => new EdmToClrEvaluator(null).EvaluateToClrValue <EnumInt>(GetIntegerExpression("PersonValueAnnotation2"))); #if !SILVERLIGHT this.ValidateClrObjectConverter(this.GetValueAnnotations(edmModel, edmModel.FindType("NS1.Person"), "PersonValueAnnotation8").Single(), new ClassWithEnum() { EnumInt = (EnumInt)10, }); #endif }
/// <summary> /// Create an instance of a CLR type based on <see cref="IEdmValue"/> and <see cref="Type"/>. /// </summary> /// <param name="edmValue">The <see cref="IEdmStructuredValue"/> for which the <paramref name="objectInstance"/> needs to be created.</param> /// <param name="clrType">The CLR type of the object instance.</param> /// <param name="converter">The converter instance calling this method.</param> /// <param name="objectInstance">A CLR object instance created for the <paramref name="edmValue"/></param> /// <param name="objectInstanceInitialized">True if all properties of the created <paramref name="objectInstance"/> are initialized. /// False if properties of the created instance should be initialized using the default <see cref="EdmToClrConverter"/> logic.</param> /// <returns>True if the <paramref name="objectInstance"/> is created, else false.</returns> internal bool TryCreateObjectInstance(IEdmStructuredValue edmValue, Type clrType, EdmToClrConverter converter, out object objectInstance, out bool objectInstanceInitialized) { return(TryCreateClientObjectInstance(this.dataServiceContext, edmValue, clrType, out objectInstance, out objectInstanceInitialized)); }
public EdmToClrEvaluator(IDictionary <IEdmFunction, Func <IEdmValue[], IEdmValue> > builtInFunctions, Func <string, IEdmValue[], IEdmValue> lastChanceFunctionApplier) : base(builtInFunctions, lastChanceFunctionApplier) { this.edmToClrConverter = new EdmToClrConverter(); }
public EdmToClrEvaluator(IDictionary <IEdmFunction, Func <IEdmValue[], IEdmValue> > builtInFunctions) : base(builtInFunctions) { this.edmToClrConverter = new EdmToClrConverter(); }