EdmToClrConverter

We found 10 examples in language CSharp for this search. You will see 34 fragments of code.

		// Token: 0x06000425 RID: 1061 RVA: 0x0000B46C File Offset: 0x0000966C
		public T EvaluateToClrValue<T>(IEdmExpression expression)
		{
			IEdmValue edmValue = base.Evaluate(expression);
			return this.edmToClrConverter.AsClrValue<T>(edmValue);
		}

		// Token: 0x06000426 RID: 1062 RVA: 0x0000B490 File Offset: 0x00009690
		public T EvaluateToClrValue<T>(IEdmExpression expression, IEdmStructuredValue context)
		{
			IEdmValue edmValue = base.Evaluate(expression, context);
			return this.edmToClrConverter.AsClrValue<T>(edmValue);
		}

		// Token: 0x06000427 RID: 1063 RVA: 0x0000B4B4 File Offset: 0x000096B4
		public T EvaluateToClrValue<T>(IEdmExpression expression, IEdmStructuredValue context, IEdmTypeReference targetType)
		{
			IEdmValue edmValue = base.Evaluate(expression, context, targetType);
			return this.edmToClrConverter.AsClrValue<T>(edmValue);
		}


		// Token: 0x060003DF RID: 991 RVA: 0x00009BD3 File Offset: 0x00007DD3
		internal static char AsClrChar(IEdmValue edmValue)
		{
			return checked((char)EdmToClrConverter.AsClrInt64(edmValue));
		}

		// Token: 0x060003E0 RID: 992 RVA: 0x00009BDC File Offset: 0x00007DDC
		internal static byte AsClrByte(IEdmValue edmValue)
		{
			return checked((byte)EdmToClrConverter.AsClrInt64(edmValue));
		}

		// Token: 0x060003E1 RID: 993 RVA: 0x00009BE5 File Offset: 0x00007DE5
		internal static short AsClrInt16(IEdmValue edmValue)
		{
			return checked((short)EdmToClrConverter.AsClrInt64(edmValue));
		}

		// Token: 0x060003E2 RID: 994 RVA: 0x00009BEE File Offset: 0x00007DEE
		internal static int AsClrInt32(IEdmValue edmValue)
		{
			return checked((int)EdmToClrConverter.AsClrInt64(edmValue));
		}

		// Token: 0x060003E4 RID: 996 RVA: 0x00009C10 File Offset: 0x00007E10
		internal static float AsClrSingle(IEdmValue edmValue)
		{
			return (float)EdmToClrConverter.AsClrDouble(edmValue);
		}


        /// <summary>
        /// Evaluates an expression with no value context.
        /// </summary>
        /// <typeparam name="T">The CLR type of the value to be returned.</typeparam>
        /// <param name="expression">Expression to evaluate. The expression must not contain paths, because no context for evaluating a path is supplied.</param>
        /// <returns>The value that results from evaluating the expression in the context of the supplied value.</returns>
        public T EvaluateToClrValue<T>(IEdmExpression expression)
        {
            IEdmValue edmValue = this.Evaluate(expression);
            return this.edmToClrConverter.AsClrValue<T>(edmValue);
        }
        
        /// <summary>
        /// Evaluates an expression in the context of a value.
        /// </summary>
        /// <typeparam name="T">The CLR type of the value to be returned.</typeparam>
        /// <param name="expression">Expression to evaluate.</param>
        /// <param name="context">Value to use as context in evaluating the expression.</param>
        /// <returns>The value that results from evaluating the expression in the context of the supplied value.</returns>
        public T EvaluateToClrValue<T>(IEdmExpression expression, IEdmStructuredValue context)
        {
            IEdmValue edmValue = this.Evaluate(expression, context);
            return this.edmToClrConverter.AsClrValue<T>(edmValue);
        }

        /// <summary>
        /// Evaluates an expression in the context of a value and a target type.
        /// </summary>
        /// <typeparam name="T">The CLR type of the value to be returned.</typeparam>
        /// <param name="expression">Expression to evaluate.</param>
        /// <param name="context">Value to use as context in evaluating the expression.</param>
        /// <param name="targetType">Type to which the result value is expected to conform.</param>
        /// <returns>The value that results from evaluating the expression in the context of the supplied value, asserted to be of the targetType.</returns>
        public T EvaluateToClrValue<T>(IEdmExpression expression, IEdmStructuredValue context, IEdmTypeReference targetType)
        {
            IEdmValue edmValue = this.Evaluate(expression, context, targetType);
            return this.edmToClrConverter.AsClrValue<T>(edmValue);
        }


        /// <summary>
        /// Evaluates an expression with no value context.
        /// </summary>
        /// <typeparam name="T">The CLR type of the value to be returned.</typeparam>
        /// <param name="expression">Expression to evaluate. The expression must not contain paths, because no context for evaluating a path is supplied.</param>
        /// <returns>The value that results from evaluating the expression in the context of the supplied value.</returns>
        public T EvaluateToClrValue<T>(IEdmExpression expression)
        {
            IEdmValue edmValue = this.Evaluate(expression);
            return this.edmToClrConverter.AsClrValue<T>(edmValue);
        }
        
        /// <summary>
        /// Evaluates an expression in the context of a value.
        /// </summary>
        /// <typeparam name="T">The CLR type of the value to be returned.</typeparam>
        /// <param name="expression">Expression to evaluate.</param>
        /// <param name="context">Value to use as context in evaluating the expression.</param>
        /// <returns>The value that results from evaluating the expression in the context of the supplied value.</returns>
        public T EvaluateToClrValue<T>(IEdmExpression expression, IEdmStructuredValue context)
        {
            IEdmValue edmValue = this.Evaluate(expression, context);
            return this.edmToClrConverter.AsClrValue<T>(edmValue);
        }

        /// <summary>
        /// Evaluates an expression in the context of a value and a target type.
        /// </summary>
        /// <typeparam name="T">The CLR type of the value to be returned.</typeparam>
        /// <param name="expression">Expression to evaluate.</param>
        /// <param name="context">Value to use as context in evaluating the expression.</param>
        /// <param name="targetType">Type to which the result value is expected to conform.</param>
        /// <returns>The value that results from evaluating the expression in the context of the supplied value, asserted to be of the targetType.</returns>
        public T EvaluateToClrValue<T>(IEdmExpression expression, IEdmStructuredValue context, IEdmTypeReference targetType)
        {
            IEdmValue edmValue = this.Evaluate(expression, context, targetType);
            return this.edmToClrConverter.AsClrValue<T>(edmValue);
        }


        /// <summary>
        /// Evaluates an expression with no value context.
        /// </summary>
        /// <typeparam name="T">The CLR type of the value to be returned.</typeparam>
        /// <param name="expression">Expression to evaluate. The expression must not contain paths, because no context for evaluating a path is supplied.</param>
        /// <returns>The value that results from evaluating the expression in the context of the supplied value.</returns>
        public T EvaluateToClrValue<T>(IEdmExpression expression)
        {
            IEdmValue edmValue = this.Evaluate(expression);
            return this.edmToClrConverter.AsClrValue<T>(edmValue);
        }
        
        /// <summary>
        /// Evaluates an expression in the context of a value.
        /// </summary>
        /// <typeparam name="T">The CLR type of the value to be returned.</typeparam>
        /// <param name="expression">Expression to evaluate.</param>
        /// <param name="context">Value to use as context in evaluating the expression.</param>
        /// <returns>The value that results from evaluating the expression in the context of the supplied value.</returns>
        public T EvaluateToClrValue<T>(IEdmExpression expression, IEdmStructuredValue context)
        {
            IEdmValue edmValue = this.Evaluate(expression, context);
            return this.edmToClrConverter.AsClrValue<T>(edmValue);
        }

        /// <summary>
        /// Evaluates an expression in the context of a value and a target type.
        /// </summary>
        /// <typeparam name="T">The CLR type of the value to be returned.</typeparam>
        /// <param name="expression">Expression to evaluate.</param>
        /// <param name="context">Value to use as context in evaluating the expression.</param>
        /// <param name="targetType">Type to which the result value is expected to conform.</param>
        /// <returns>The value that results from evaluating the expression in the context of the supplied value, asserted to be of the targetType.</returns>
        public T EvaluateToClrValue<T>(IEdmExpression expression, IEdmStructuredValue context, IEdmTypeReference targetType)
        {
            IEdmValue edmValue = this.Evaluate(expression, context, targetType);
            return this.edmToClrConverter.AsClrValue<T>(edmValue);
        }


		public T EvaluateToClrValue<T>(IEdmExpression expression)
		{
			IEdmValue edmValue = base.Evaluate(expression);
			return this.edmToClrConverter.AsClrValue<T>(edmValue);
		}

		public T EvaluateToClrValue<T>(IEdmExpression expression, IEdmStructuredValue context)
		{
			IEdmValue edmValue = base.Evaluate(expression, context);
			return this.edmToClrConverter.AsClrValue<T>(edmValue);
		}

		public T EvaluateToClrValue<T>(IEdmExpression expression, IEdmStructuredValue context, IEdmTypeReference targetType)
		{
			IEdmValue edmValue = base.Evaluate(expression, context, targetType);
			return this.edmToClrConverter.AsClrValue<T>(edmValue);
		}

//   OData .NET Libraries
//   Copyright (c) Microsoft Corporation
//   All rights reserved. 

//   Licensed under the Apache License, Version 2.0 (the ""License""); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 

//   THIS CODE IS PROVIDED ON AN  *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 

//   See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.

using System;
using Microsoft.OData.Edm.Values;

namespace Microsoft.OData.Edm.EdmToClrConversion
{
    /// <summary>
    /// Represents a delegate for creating an instance of CLR type based on <see cref="IEdmValue"/> and <see cref="Type"/>.
    /// The delegate can be used to create CLR instances of polymorphic types.
    /// </summary>
    /// <param name="edmValue">The <see cref="IEdmStructuredValue"/> for which the <paramref name="objectInstance"/> needs to be created.</param>
    /// <param name="clrType">The expected CLR type of the object instance. In case of polymorphic properties and collections this may be a base type.</param>
    /// <param name="converter">The converter instance calling this delegate.</param>
    /// <param name="objectInstance">The output parameter returning a CLR object instance created for the <paramref name="edmValue"/>.</param>
    /// <param name="objectInstanceInitialized">The output parameter returning 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 delegate produced a desired <paramref name="objectInstance"/>.
    /// If delegate returns false, the default <see cref="EdmToClrConverter"/> logic will be applied to create and populate a CLR object instance.</returns>
    public delegate bool TryCreateObjectInstance(
        IEdmStructuredValue edmValue,
        Type clrType,
        EdmToClrConverter converter,
        out object objectInstance,
        out bool objectInstanceInitialized);
}


		internal static bool AsClrBoolean(IEdmValue edmValue)
		{
			EdmUtil.CheckArgumentNull<IEdmValue>(edmValue, "edmValue");
			return ((IEdmBooleanValue)edmValue).Value;
		}

		internal static byte AsClrByte(IEdmValue edmValue)
		{
			return (byte)EdmToClrConverter.AsClrInt64(edmValue);
		}

		internal static byte[] AsClrByteArray(IEdmValue edmValue)
		{
			EdmUtil.CheckArgumentNull<IEdmValue>(edmValue, "edmValue");
			if (edmValue as IEdmNullValue == null)
			{
				return ((IEdmBinaryValue)edmValue).Value;
			}
			else
			{
				return null;
			}
		}

		internal static char AsClrChar(IEdmValue edmValue)
		{
			return Convert.ToChar (EdmToClrConverter.AsClrInt64(edmValue));
		}

		internal static DateTime AsClrDateTime(IEdmValue edmValue)
		{
			EdmUtil.CheckArgumentNull<IEdmValue>(edmValue, "edmValue");
			return ((IEdmDateTimeValue)edmValue).Value;
		}

		internal static DateTimeOffset AsClrDateTimeOffset(IEdmValue edmValue)
		{
			EdmUtil.CheckArgumentNull<IEdmValue>(edmValue, "edmValue");
			return ((IEdmDateTimeOffsetValue)edmValue).Value;
		}

        [TestMethod]
        public void ConvertPrimitiveTypes()
        {
            EdmToClrConverter c = new EdmToClrConverter();

            byte[] b = { 1, 2, 3 };
            Assert.AreEqual(b, c.AsClrValue(new EdmBinaryConstant(b), typeof(byte[])), "binary 1");
            Assert.AreEqual(b, c.AsClrValue<byte[]>(new EdmBinaryConstant(b)), "binary2");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(byte[])), "Null is null even when null is bool");

            Assert.AreEqual("qwerty", c.AsClrValue(new EdmStringConstant("qwerty"), typeof(string)), "string 1");
            Assert.AreEqual("qwerty", c.AsClrValue<string>(new EdmStringConstant("qwerty")), "string 2");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(string)), "Null is null even when null is string");

            Assert.AreEqual(true, c.AsClrValue(new EdmBooleanConstant(true), typeof(bool)), "bool 1");
            Assert.AreEqual(true, c.AsClrValue<bool>(new EdmBooleanConstant(true)), "bool 2");

            bool? nullableBool = true;
            Assert.AreEqual(nullableBool, c.AsClrValue(new EdmBooleanConstant(true), typeof(bool?)), "nullable bool");
            Assert.AreEqual(typeof(bool), c.AsClrValue(new EdmBooleanConstant(true), typeof(bool?)).GetType(), "nullable bool correct type");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(bool?)), "Null is null even when null is bool");

            Assert.AreEqual('q', c.AsClrValue(new EdmIntegerConstant('q'), typeof(char)), "char 1");
            Assert.AreEqual('q', c.AsClrValue<char>(new EdmIntegerConstant('q')), "char 2");

            char? nullableChar = 'q';
            Assert.AreEqual(nullableChar, c.AsClrValue(new EdmIntegerConstant('q'), typeof(char?)), "nullable char");
            Assert.AreEqual(typeof(char), c.AsClrValue(new EdmIntegerConstant('q'), typeof(char?)).GetType(), "nullable char is correct type");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(char?)), "Null is null even when null is char");

            Assert.AreEqual((sbyte)(-12), c.AsClrValue(new EdmIntegerConstant(-12), typeof(sbyte)), "sbyte 1");
            Assert.AreEqual((sbyte)(-12), c.AsClrValue<sbyte>(new EdmIntegerConstant(-12)), "sbyte 2");

            sbyte? nullableSByte = -12;
            Assert.AreEqual(nullableSByte, ((sbyte?)c.AsClrValue(new EdmIntegerConstant(-12), typeof(sbyte?))).Value, "nullable sbyte");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(sbyte?)), "Null is null even when null is sbyte");

            Assert.AreEqual((byte)12, c.AsClrValue(new EdmIntegerConstant(12), typeof(byte)), "byte 1");
            Assert.AreEqual((byte)12, c.AsClrValue<byte>(new EdmIntegerConstant(12)), "byte 2");
            try
            {
                c.AsClrValue<byte>(new EdmIntegerConstant(257));
                Assert.Fail("OverflowException expected");
            }
            catch (OverflowException)
            {
            }

            byte? nullableByte = 12;
            Assert.AreEqual(nullableByte, ((byte?)c.AsClrValue(new EdmIntegerConstant(12), typeof(byte?))).Value, "nullable byte");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(byte?)), "Null is null even when null is byte");
            try
            {
                c.AsClrValue(new EdmIntegerConstant(257), typeof(byte?));
                Assert.Fail("OverflowException expected");
            }
            catch (OverflowException)
            {
            }

            Assert.AreEqual((Int16)(-12), c.AsClrValue(new EdmIntegerConstant(-12), typeof(Int16)), "Int16 1");
            Assert.AreEqual((Int16)(-12), c.AsClrValue<Int16>(new EdmIntegerConstant(-12)), "Int16 2");

            Int16? nullableInt16 = -12;
            Assert.AreEqual(nullableInt16, ((Int16?)c.AsClrValue(new EdmIntegerConstant(-12), typeof(Int16?))).Value, "nullable Int16");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(Int16?)), "Null is null even when null is Int16");

            Assert.AreEqual((UInt16)12, c.AsClrValue(new EdmIntegerConstant(12), typeof(UInt16)), "UInt16 1");
            Assert.AreEqual((UInt16)12, c.AsClrValue<UInt16>(new EdmIntegerConstant(12)), "UInt16 2");

            Assert.AreEqual((UInt16)12, ((UInt16?)c.AsClrValue(new EdmIntegerConstant(12), typeof(UInt16?))).Value, "nullable UInt16");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(UInt16?)), "Null is null even when null is UInt16");

            Assert.AreEqual((Int32)(-12), c.AsClrValue(new EdmIntegerConstant(-12), typeof(Int32)), "Int32 1");
            Assert.AreEqual((Int32)(-12), c.AsClrValue<Int32>(new EdmIntegerConstant(-12)), "Int32 2");

            Assert.AreEqual((Int32)(-12), ((Int32?)c.AsClrValue(new EdmIntegerConstant(-12), typeof(Int32?))).Value, "nullable Int32");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(Int32?)), "Null is null even when null is Int32");

            Assert.AreEqual((UInt32)12, c.AsClrValue(new EdmIntegerConstant(12), typeof(UInt32)), "UInt32 1");
            Assert.AreEqual((UInt32)12, c.AsClrValue<UInt32>(new EdmIntegerConstant(12)), "UInt32 2");

            Assert.AreEqual((UInt32)12, ((UInt32?)c.AsClrValue(new EdmIntegerConstant(12), typeof(UInt32?))).Value, "nullable UInt32");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(UInt32?)), "Null is null even when null is UInt32");

            Assert.AreEqual((Int64)(-12), c.AsClrValue(new EdmIntegerConstant(-12), typeof(Int64)), "Int64 1");
            Assert.AreEqual((Int64)(-12), c.AsClrValue<Int64>(new EdmIntegerConstant(-12)), "Int64 2");

            Assert.AreEqual((Int64)(-12), ((Int64?)c.AsClrValue(new EdmIntegerConstant(-12), typeof(Int64?))).Value, "nullable Int64");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(Int64?)), "Null is null even when null is Int64");

            Assert.AreEqual((UInt64)12, c.AsClrValue(new EdmIntegerConstant(12), typeof(UInt64)), "UInt64 1");
            Assert.AreEqual((UInt64)12, c.AsClrValue<UInt64>(new EdmIntegerConstant(12)), "UInt64 2");

            Assert.AreEqual((UInt64)12, ((UInt64?)c.AsClrValue(new EdmIntegerConstant(12), typeof(UInt64?))).Value, "nullable UInt64");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(UInt64?)), "Null is null even when null is UInt64");

            Assert.AreEqual((double)1.2, c.AsClrValue(new EdmFloatingConstant(1.2), typeof(double)), "double 1");
            Assert.AreEqual((double)1.2, c.AsClrValue<double>(new EdmFloatingConstant(1.2)), "double 2");

            Assert.AreEqual((double)1.2, ((double?)c.AsClrValue(new EdmFloatingConstant(1.2), typeof(double?))).Value, "nullable double");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(double?)), "Null is null even when null is double");

            Assert.AreEqual((Single)1.2, c.AsClrValue(new EdmFloatingConstant(1.2), typeof(Single)), "Single 1");
            Assert.AreEqual((Single)1.2, c.AsClrValue<Single>(new EdmFloatingConstant(1.2)), "Single 2");

            Assert.AreEqual((Single)1.2, ((Single?)c.AsClrValue(new EdmFloatingConstant(1.2), typeof(Single?))).Value, "nullable Single");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(Single?)), "Null is null even when null is Single");

            Assert.AreEqual((decimal)1.2, c.AsClrValue(new EdmDecimalConstant(1.2m), typeof(decimal)), "decimal 1");
            Assert.AreEqual((decimal)1.2, c.AsClrValue<decimal>(new EdmDecimalConstant(1.2m)), "decimal 2");

            Assert.AreEqual((decimal)1.2, ((decimal?)c.AsClrValue(new EdmDecimalConstant(1.2m), typeof(decimal?))).Value, "nullable decimal");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(decimal?)), "Null is null even when null is decimal");

            DateTime dt = new DateTime(2011, 10, 18, 14, 48, 16);

            DateTimeOffset dto = new DateTimeOffset(dt, new TimeSpan(-8, 0, 0));
            Assert.AreEqual(dto, c.AsClrValue(new EdmDateTimeOffsetConstant(dto), typeof(DateTimeOffset)), "DateTimeOffset 1");
            Assert.AreEqual(dto, c.AsClrValue<DateTimeOffset>(new EdmDateTimeOffsetConstant(dto)), "DateTimeOffset 2");

            Assert.AreEqual(dto, ((DateTimeOffset?)c.AsClrValue(new EdmDateTimeOffsetConstant(dto), typeof(DateTimeOffset?))).Value, "nullable DateTimeOffset");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(DateTimeOffset?)), "Null is null even when null is DateTimeOffset");

            Date date = new Date(2014, 8, 8);

            Assert.AreEqual(date, c.AsClrValue(new EdmDateConstant(date), typeof(Date)), "Date 1");
            Assert.AreEqual(date, c.AsClrValue<Date>(new EdmDateConstant(date)), "Date 2");
            Assert.AreEqual(date, ((Date?)c.AsClrValue(new EdmDateConstant(date), typeof(Date?))).Value, "nullable Date");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(Date?)), "Null is null even when null is Date");

            TimeOfDay time = new TimeOfDay(12, 5, 30, 900);
            Assert.AreEqual(time, c.AsClrValue(new EdmTimeOfDayConstant(time), typeof(TimeOfDay)), "Time 1");
            Assert.AreEqual(time, c.AsClrValue<TimeOfDay>(new EdmTimeOfDayConstant(time)), "Time 2");
            Assert.AreEqual(time, ((TimeOfDay?)c.AsClrValue(new EdmTimeOfDayConstant(time), typeof(TimeOfDay?))).Value, "nullable Time");
            Assert.IsNull(c.AsClrValue(EdmNullExpression.Instance, typeof(TimeOfDay?)), "Null is null even when null is Time");
        }

        [TestMethod]
        public void ConvertCollections()
        {
            string[] elements = { "ab", "cd", "ef", null };
            EdmCollectionValue c = new EdmCollectionValue(
                EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetString(true)),
                elements.Select(e => e == null ? (IEdmDelayedValue)EdmNullExpression.Instance : (IEdmDelayedValue)new EdmStringConstant(e)));

            EdmToClrConverter cv = new EdmToClrConverter();

            string[] converted = ((IEnumerable<string>)cv.AsClrValue(c, typeof(IEnumerable<string>))).ToArray();
            Assert.AreEqual(elements.Length, converted.Length, "Length 1");
            for (int i = 0; i < elements.Length; ++i)
            {
                Assert.AreEqual(elements[i], converted[i], "elements[i] 1");
            }

            converted = cv.AsClrValue<IEnumerable<string>>(c).ToArray();
            Assert.AreEqual(elements.Length, converted.Length, "Length 2");
            for (int i = 0; i < elements.Length; ++i)
            {
                Assert.AreEqual(elements[i], converted[i], "elements[i] 2");
            }
        }

        [TestMethod]
        public void ConvertEnums()
        {
            EdmToClrConverter c = new EdmToClrConverter();

            EdmEnumType e32 = new EdmEnumType("", "");
            var r32 = e32.AddMember("Red", new EdmEnumMemberValue(10));
            e32.AddMember("Blue", new EdmEnumMemberValue(20));

            EdmEnumValue evRed = new EdmEnumValue(new EdmEnumTypeReference(e32, false), r32);
            EdmEnumValue ev20 = new EdmEnumValue(new EdmEnumTypeReference(e32, false), new EdmEnumMemberValue(20));
            EdmEnumValue ev30 = new EdmEnumValue(new EdmEnumTypeReference(e32, false), new EdmEnumMemberValue(30));

            Assert.AreEqual(RedBlue.Red, c.AsClrValue<RedBlue>(evRed), "c.AsClrValue<RedBlue>(evRed)");
            Assert.AreEqual(RedBlue.Blue, c.AsClrValue<RedBlue>(ev20), "c.AsClrValue<RedBlue>(ev20)");
            Assert.AreEqual((RedBlue)30, c.AsClrValue<RedBlue>(ev30), "c.AsClrValue<RedBlue>(ev30)");

            Assert.AreEqual(null, c.AsClrValue<RedBlue?>(EdmNullExpression.Instance), "Null Enum is null");

            Assert.AreEqual(RedBlueByte.Red, c.AsClrValue<RedBlueByte>(evRed), "c.AsClrValue<RedBlueByte>(evRed)");
            Assert.AreEqual(RedBlueByte.Blue, c.AsClrValue<RedBlueByte>(ev20), "c.AsClrValue<RedBlueByte>(ev20)");
            Assert.AreEqual((RedBlueByte)30, c.AsClrValue<RedBlueByte>(ev30), "c.AsClrValue<RedBlueByte>(ev30)");

            Assert.AreEqual(RedBlueLong.Red, c.AsClrValue<RedBlueLong>(evRed), "c.AsClrValue<RedBlueLong>(evRed)");
            Assert.AreEqual(RedBlueLong.Blue, c.AsClrValue<RedBlueLong>(ev20), "c.AsClrValue<RedBlueLong>(ev20)");
            Assert.AreEqual((RedBlueLong)30, c.AsClrValue<RedBlueLong>(ev30), "c.AsClrValue<RedBlueLong>(ev30)");

            Assert.AreEqual(RedBlueLong.Red, c.AsClrValue(evRed, typeof(RedBlueLong)), "c.AsClrValue(evRed, typeof(RedBlueLong))");
            Assert.AreEqual(RedBlueLong.Blue, c.AsClrValue(ev20, typeof(RedBlueLong)), "c.AsClrValue(ev20, typeof(RedBlueLong))");
            Assert.AreEqual((RedBlueLong)30, c.AsClrValue(ev30, typeof(RedBlueLong)), "c.AsClrValue(ev30, typeof(RedBlueLong))");
        }

        [TestMethod]
        public void ConvertStructuredTypes()
        {
            IEdmStructuredValue c1_inner = new EdmStructuredValue(null, new IEdmPropertyValue[]
            {
                new EdmPropertyValue("P1", new EdmStringConstant("c1ip1")),
                new EdmPropertyValue("P3", new EdmIntegerConstant(110))
            });

            IEdmStructuredValue c2 = new EdmStructuredValue(null, new IEdmPropertyValue[]
            {
                new EdmPropertyValue("Q1", new EdmStringConstant("c2q1")),
                new EdmPropertyValue("Q2", c1_inner),
                new EdmPropertyValue("Q3", new EdmIntegerConstant(22))
            });

            IEdmStructuredValue c1 = new EdmStructuredValue(null, new IEdmPropertyValue[]
            {
                new EdmPropertyValue("P1", new EdmStringConstant("c1p1")),
                new EdmPropertyValue("P2", c2),
                new EdmPropertyValue("P3", new EdmIntegerConstant(11))
            });

            EdmToClrConverter cv = new EdmToClrConverter();

            C1[] clrC1s = { (C1)cv.AsClrValue(c1, typeof(C1)), cv.AsClrValue<C1>(c1) };

            for (int i = 0; i < clrC1s.Length; ++i)
            {
                C1 clrC1 = clrC1s[i];

                // C1
                Assert.IsNotNull(clrC1, "c1 " + i);
                Assert.AreEqual("c1p1", clrC1.P1, "c1.P1 " + i);
                Assert.AreEqual(11, clrC1.P3, "c1.P3 " + i);

                // C1.P2
                Assert.IsNotNull(clrC1.P2, "c1.P2 " + i);
                Assert.AreEqual("c2q1", clrC1.P2.Q1, "c1.P2.Q1 " + i);
                Assert.AreEqual(22, clrC1.P2.Q3, "c1.P2.Q3 " + i);

                // C1.P2.Q2
                Assert.IsNotNull(clrC1.P2.Q2, "c1.P2.Q2 " + i);
                Assert.AreEqual("c1ip1", clrC1.P2.Q2.P1, "c1.P2.Q2.P1 " + i);
                Assert.AreEqual(110, clrC1.P2.Q2.P3, "c1.P2.Q2.P3 " + i);

                // C1.P2.Q2.P2
                Assert.IsNull(clrC1.P2.Q2.P2, "c1.P2.Q2.P2 " + i);
            }
        }

        [TestMethod]
        public void ConvertGraphWithLoops()
        {
            var c1_inner_loopProperty = new EdmPropertyValue("P2");

            IEdmStructuredValue c1_inner = new EdmStructuredValue(null, new IEdmPropertyValue[]
            {
                new EdmPropertyValue("P1", new EdmStringConstant("c1ip1")),
                c1_inner_loopProperty,
                new EdmPropertyValue("P3", new EdmIntegerConstant(110)),
                new EdmPropertyValue("NoClrCounterpart", new EdmIntegerConstant(110)) // conversion should ignore this and not choke
            });

            IEdmStructuredValue c2 = new EdmStructuredValue(null, new IEdmPropertyValue[]
            {
                new EdmPropertyValue("Q1", new EdmStringConstant("c2q1")),
                new EdmPropertyValue("Q2", c1_inner),
                new EdmPropertyValue("Q3", EdmNullExpression.Instance)
            });

            c1_inner_loopProperty.Value = c2;

            IEdmStructuredValue c1 = new EdmStructuredValue(null, new IEdmPropertyValue[]
            {
                new EdmPropertyValue("P1", new EdmStringConstant("c1p1")),
                new EdmPropertyValue("P2", c2),
                new EdmPropertyValue("P3", new EdmIntegerConstant(11))
            });

            EdmToClrConverter cv = new EdmToClrConverter();

            C1[] clrC1s = { (C1)cv.AsClrValue(c1, typeof(C1)), cv.AsClrValue<C1>(c1) };

            for (int i = 0; i < clrC1s.Length; ++i)
            {
                C1 clrC1 = clrC1s[i];

                // C1
                Assert.IsNotNull(clrC1, "c1 " + i);
                Assert.AreEqual("c1p1", clrC1.P1, "c1.P1 " + i);
                Assert.AreEqual(11, clrC1.P3, "c1.P3 " + i);

                // C1.P2
                Assert.IsNotNull(clrC1.P2, "c1.P2 " + i);
                Assert.AreEqual("c2q1", clrC1.P2.Q1, "c1.P2.Q1 " + i);
                Assert.AreEqual(null, clrC1.P2.Q3, "c1.P2.Q3 " + i);

                // C1.P2.Q2
                Assert.IsNotNull(clrC1.P2.Q2, "c1.P2.Q2 " + i);
                Assert.AreEqual("c1ip1", clrC1.P2.Q2.P1, "c1.P2.Q2.P1 " + i);
                Assert.AreEqual(110, clrC1.P2.Q2.P3, "c1.P2.Q2.P3 " + i);

                // C1.P2.Q2.P2
                Assert.IsTrue(Object.ReferenceEquals(clrC1.P2.Q2.P2, clrC1.P2), "c1.P2.Q2.P2 " + i);
            }
        }
            #region IList<int>
            int IList<int>.IndexOf(int item)
            {
                throw new NotImplementedException();
            }

//   OData .NET Libraries
//   Copyright (c) Microsoft Corporation
//   All rights reserved. 

//   Licensed under the Apache License, Version 2.0 (the ""License""); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 

//   THIS CODE IS PROVIDED ON AN  *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 

//   See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.

using System;
using Microsoft.OData.Edm.Values;

namespace Microsoft.OData.Edm.EdmToClrConversion
{
    /// <summary>
    /// Represents a delegate for creating an instance of CLR type based on <see cref="IEdmValue"/> and <see cref="Type"/>.
    /// The delegate can be used to create CLR instances of polymorphic types.
    /// </summary>
    /// <param name="edmValue">The <see cref="IEdmStructuredValue"/> for which the <paramref name="objectInstance"/> needs to be created.</param>
    /// <param name="clrType">The expected CLR type of the object instance. In case of polymorphic properties and collections this may be a base type.</param>
    /// <param name="converter">The converter instance calling this delegate.</param>
    /// <param name="objectInstance">The output parameter returning a CLR object instance created for the <paramref name="edmValue"/>.</param>
    /// <param name="objectInstanceInitialized">The output parameter returning 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 delegate produced a desired <paramref name="objectInstance"/>.
    /// If delegate returns false, the default <see cref="EdmToClrConverter"/> logic will be applied to create and populate a CLR object instance.</returns>
    public delegate bool TryCreateObjectInstance(
        IEdmStructuredValue edmValue,
        Type clrType,
        EdmToClrConverter converter,
        out object objectInstance,
        out bool objectInstanceInitialized);
}

Microsoft.Data.Edm.EdmToClrConversion.EdmToClrConverter : Object

Constructors :

public EdmToClrConverter()
public EdmToClrConverter(TryCreateObjectInstance tryCreateObjectInstanceDelegate = )

Methods :

public T AsClrValue(IEdmValue edmValue = )
public Object AsClrValue(IEdmValue edmValue = , Type clrType = )
public Void RegisterConvertedObject(IEdmStructuredValue edmValue = , Object clrObject = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()