EdmValue

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

        private IEnumerable AsIEnumerable(IEdmValue edmValue, Type elementType)
        {
            // By convention we only support converting from a collection value.
            foreach (IEdmDelayedValue element in ((IEdmCollectionValue)edmValue).Elements)
            {
                yield return this.AsClrValue(element.Value, elementType);
            }
        }


        private IEnumerable AsIEnumerable(IEdmValue edmValue, Type elementType)
        {
            // By convention we only support converting from a collection value.
            foreach (IEdmDelayedValue element in ((IEdmCollectionValue)edmValue).Elements)
            {
                yield return this.AsClrValue(element.Value, elementType);
            }
        }


        private IEnumerable AsIEnumerable(IEdmValue edmValue, Type elementType)
        {
            // By convention we only support converting from a collection value.
            foreach (IEdmDelayedValue element in ((IEdmCollectionValue)edmValue).Elements)
            {
                yield return this.AsClrValue(element.Value, elementType);
            }
        }


        private IEnumerable AsIEnumerable(IEdmValue edmValue, Type elementType)
        {
            // By convention we only support converting from a collection value.
            foreach (IEdmDelayedValue element in ((IEdmCollectionValue)edmValue).Elements)
            {
                yield return this.AsClrValue(element.Value, elementType);
            }
        }


        private IEnumerable AsIEnumerable(IEdmValue edmValue, Type elementType)
        {
            // By convention we only support converting from a collection value.
            foreach (IEdmDelayedValue element in ((IEdmCollectionValue)edmValue).Elements)
            {
                yield return this.AsClrValue(element.Value, elementType);
            }
        }


		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;
		}

        /// <summary>
        /// Converts a primitive OData value to the corresponding <see cref="IEdmDelayedValue"/>.
        /// </summary>
        /// <param name="primitiveValue">The primitive OData value to convert.</param>
        /// <param name="type">The <see cref="IEdmTypeReference"/> for the primitive value (if available).</param>
        /// <returns>An <see cref="IEdmDelayedValue"/> for the <paramref name="primitiveValue"/>.</returns>
        internal static IEdmDelayedValue ConvertPrimitiveValue(object primitiveValue, IEdmPrimitiveTypeReference type)
        {
#if !ASTORIA_CLIENT
#endif
            Debug.Assert(primitiveValue != null, "primitiveValue != null");

            TypeCode typeCode = PlatformHelpers.GetTypeCode(primitiveValue.GetType());
            switch (typeCode)
            {
                case TypeCode.Boolean:
                    type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Boolean);
                    return new EdmBooleanConstant(type, (bool)primitiveValue);

                case TypeCode.Byte:
                    type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Byte);
                    return new EdmIntegerConstant(type, (byte)primitiveValue);

                case TypeCode.SByte:
                    type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.SByte);
                    return new EdmIntegerConstant(type, (sbyte)primitiveValue);

                case TypeCode.Int16:
                    type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Int16);
                    return new EdmIntegerConstant(type, (Int16)primitiveValue);

                case TypeCode.Int32:
                    type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Int32);
                    return new EdmIntegerConstant(type, (Int32)primitiveValue);

                case TypeCode.Int64:
                    type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Int64);
                    return new EdmIntegerConstant(type, (Int64)primitiveValue);

                case TypeCode.Decimal:
                    IEdmDecimalTypeReference decimalType = (IEdmDecimalTypeReference)EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Decimal);
                    return new EdmDecimalConstant(decimalType, (decimal)primitiveValue);

                case TypeCode.Single:
                    type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Single);
                    return new EdmFloatingConstant(type, (Single)primitiveValue);

                case TypeCode.Double:
                    type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Double);
                    return new EdmFloatingConstant(type, (double)primitiveValue);

                case TypeCode.String:
                    IEdmStringTypeReference stringType = (IEdmStringTypeReference)EnsurePrimitiveType(type, EdmPrimitiveTypeKind.String);
                    return new EdmStringConstant(stringType, (string)primitiveValue);

                default:
                    return ConvertPrimitiveValueWithoutTypeCode(primitiveValue, type);
            }
        }

        /// <summary>
        /// Gets the clr value of the edm value based on its type.
        /// </summary>
        /// <param name="edmValue">The edm value.</param>
        /// <returns>The clr value</returns>
        internal static object ToClrValue(this IEdmPrimitiveValue edmValue)
        {
#if !ASTORIA_CLIENT
#endif
            Debug.Assert(edmValue != null, "edmValue != null");
            EdmPrimitiveTypeKind primitiveKind = edmValue.Type.PrimitiveKind();
            switch (edmValue.ValueKind)
            {
                case EdmValueKind.Binary:
                    return ((IEdmBinaryValue)edmValue).Value;

                case EdmValueKind.Boolean:
                    return ((IEdmBooleanValue)edmValue).Value;

                case EdmValueKind.DateTimeOffset:
                    return ((IEdmDateTimeOffsetValue)edmValue).Value;

                case EdmValueKind.Decimal:
                    return ((IEdmDecimalValue)edmValue).Value;

                case EdmValueKind.Guid:
                    return ((IEdmGuidValue)edmValue).Value;

                case EdmValueKind.String:
                    return ((IEdmStringValue)edmValue).Value;

                case EdmValueKind.Duration:
                    return ((IEdmDurationValue)edmValue).Value;

                case EdmValueKind.Floating:
                    return ConvertFloatingValue((IEdmFloatingValue)edmValue, primitiveKind);

                case EdmValueKind.Integer:
                    return ConvertIntegerValue((IEdmIntegerValue)edmValue, primitiveKind);
            }

            throw new ODataException(ErrorStrings.EdmValueUtils_CannotConvertTypeToClrValue(edmValue.ValueKind));
        }

#if !ASTORIA_CLIENT
        /// <summary>
        /// Tries to get a stream property of the specified name.
        /// </summary>
        /// <param name="entityInstance">The instance of the entity to get the stream property for.</param>
        /// <param name="streamPropertyName">The stream property name to find.</param>
        /// <param name="streamProperty">The stream property found.</param>
        /// <returns>true if the stream property was found or if the stream property name was null (default stream).
        /// false if the stream property doesn't exist.</returns>
        internal static bool TryGetStreamProperty(IEdmStructuredValue entityInstance, string streamPropertyName, out IEdmProperty streamProperty)
        {
            Debug.Assert(entityInstance != null, "entityInstance != null");

            streamProperty = null;
            if (streamPropertyName != null)
            {
                streamProperty = entityInstance.Type.AsEntity().FindProperty(streamPropertyName);
                if (streamProperty == null)
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Gets the the CLR value for a primitive property.
        /// </summary>
        /// <param name="structuredValue">The structured value.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns>The clr value of the property.</returns>
        internal static object GetPrimitivePropertyClrValue(this IEdmStructuredValue structuredValue, string propertyName)
        {
            Debug.Assert(structuredValue != null, "entityInstance != null");
            IEdmStructuredTypeReference valueType = structuredValue.Type.AsStructured();

            IEdmPropertyValue propertyValue = structuredValue.FindPropertyValue(propertyName);
            if (propertyValue == null)
            {
                throw new ODataException(ErrorStrings.EdmValueUtils_PropertyDoesntExist(valueType.FullName(), propertyName));
            }

            if (propertyValue.Value.ValueKind == EdmValueKind.Null)
            {
                return null;
            }

            IEdmPrimitiveValue primitiveValue = propertyValue.Value as IEdmPrimitiveValue;
            if (primitiveValue == null)
            {
                throw new ODataException(ErrorStrings.EdmValueUtils_NonPrimitiveValue(propertyValue.Name, valueType.FullName()));
            }

            return primitiveValue.ToClrValue();
        }
#endif

        /// <summary>
        /// Converts a floating-point edm value to a clr value
        /// </summary>
        /// <param name="floatingValue">The edm floating-point value.</param>
        /// <param name="primitiveKind">Kind of the primitive.</param>
        /// <returns>The converted value</returns>
        private static object ConvertFloatingValue(IEdmFloatingValue floatingValue, EdmPrimitiveTypeKind primitiveKind)
        {
            Debug.Assert(floatingValue != null, "floatingValue != null");
            double doubleValue = floatingValue.Value;

            if (primitiveKind == EdmPrimitiveTypeKind.Single)
            {    
                return Convert.ToSingle(doubleValue);
            }

            Debug.Assert(primitiveKind == EdmPrimitiveTypeKind.Double, "primitiveKind == EdmPrimitiveTypeKind.Double");
            return doubleValue;
        }

        /// <summary>
        /// Converts an integer edm value to a clr value.
        /// </summary>
        /// <param name="integerValue">The integer value.</param>
        /// <param name="primitiveKind">Kind of the primitive.</param>
        /// <returns>The converted value</returns>
        private static object ConvertIntegerValue(IEdmIntegerValue integerValue, EdmPrimitiveTypeKind primitiveKind)
        {
            Debug.Assert(integerValue != null, "integerValue != null");
            long longValue = integerValue.Value;

            switch (primitiveKind)
            {
                case EdmPrimitiveTypeKind.Int16:
                    return Convert.ToInt16(longValue);

                case EdmPrimitiveTypeKind.Int32:
                    return Convert.ToInt32(longValue);

                case EdmPrimitiveTypeKind.Byte:
                    return Convert.ToByte(longValue);

                case EdmPrimitiveTypeKind.SByte:
                    return Convert.ToSByte(longValue);

                default:
                    Debug.Assert(primitiveKind == EdmPrimitiveTypeKind.Int64, "primitiveKind == EdmPrimitiveTypeKind.Int64");
                    return longValue;
            }
        }

        /// <summary>
        /// Converts a primitive OData value to the corresponding <see cref="IEdmDelayedValue"/>.
        /// </summary>
        /// <param name="primitiveValue">The primitive OData value to convert.</param>
        /// <param name="type">The <see cref="IEdmTypeReference"/> for the primitive value (if available).</param>
        /// <returns>An <see cref="IEdmDelayedValue"/> for the <paramref name="primitiveValue"/>.</returns>
        internal static IEdmDelayedValue ConvertPrimitiveValue(object primitiveValue, IEdmPrimitiveTypeReference type)
        {
            Debug.Assert(primitiveValue != null, "primitiveValue != null");

            if (primitiveValue is Boolean)
            {
                type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Boolean);
                return new EdmBooleanConstant(type, (bool)primitiveValue);
            }

            if (primitiveValue is Byte)
            {
                type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Byte);
                return new EdmIntegerConstant(type, (byte)primitiveValue);
            }

            if (primitiveValue is SByte)
            {
                type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.SByte);
                return new EdmIntegerConstant(type, (sbyte)primitiveValue);
            }

            if (primitiveValue is Int16)
            {
                type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Int16);
                return new EdmIntegerConstant(type, (Int16)primitiveValue);
            }

            if (primitiveValue is Int32)
            {
                type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Int32);
                return new EdmIntegerConstant(type, (Int32)primitiveValue);
            }

            if (primitiveValue is Int64)
            {
                type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Int64);
                return new EdmIntegerConstant(type, (Int64)primitiveValue);
            }

            if (primitiveValue is Decimal)
            {
                var decimalType =
                    (IEdmDecimalTypeReference)EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Decimal);
                return new EdmDecimalConstant(decimalType, (decimal)primitiveValue);
            }

            if (primitiveValue is Single)
            {
                type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Single);
                return new EdmFloatingConstant(type, (Single)primitiveValue);
            }

            if (primitiveValue is Double)
            {
                type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Double);
                return new EdmFloatingConstant(type, (Double)primitiveValue);
            }

            var str = primitiveValue as string;
            if (str != null)
            {
                var stringType =
                    (IEdmStringTypeReference)EnsurePrimitiveType(type, EdmPrimitiveTypeKind.String);
                return new EdmStringConstant(stringType, str);
            }

            return ConvertPrimitiveValueWithoutTypeCode(primitiveValue, type);
        }

        /// <summary>
        /// Gets the clr value of the edm value based on its type.
        /// </summary>
        /// <param name="edmValue">The edm value.</param>
        /// <returns>The clr value</returns>
        internal static object ToClrValue(this IEdmPrimitiveValue edmValue)
        {
            Debug.Assert(edmValue != null, "edmValue != null");
            EdmPrimitiveTypeKind primitiveKind = edmValue.Type.PrimitiveKind();
            switch (edmValue.ValueKind)
            {
                case EdmValueKind.Binary:
                    return ((IEdmBinaryValue)edmValue).Value;

                case EdmValueKind.Boolean:
                    return ((IEdmBooleanValue)edmValue).Value;

                case EdmValueKind.DateTimeOffset:
                    return ((IEdmDateTimeOffsetValue)edmValue).Value;

                case EdmValueKind.Decimal:
                    return ((IEdmDecimalValue)edmValue).Value;

                case EdmValueKind.Guid:
                    return ((IEdmGuidValue)edmValue).Value;

                case EdmValueKind.String:
                    return ((IEdmStringValue)edmValue).Value;

                case EdmValueKind.Duration:
                    return ((IEdmDurationValue)edmValue).Value;

                case EdmValueKind.Floating:
                    return ConvertFloatingValue((IEdmFloatingValue)edmValue, primitiveKind);

                case EdmValueKind.Integer:
                    return ConvertIntegerValue((IEdmIntegerValue)edmValue, primitiveKind);

                case EdmValueKind.Date:
                    return ((IEdmDateValue)edmValue).Value;

                case EdmValueKind.TimeOfDay:
                    return ((IEdmTimeOfDayValue)edmValue).Value;
            }

            throw new ODataException(ErrorStrings.EdmValueUtils_CannotConvertTypeToClrValue(edmValue.ValueKind));
        }

#if !ODATA_CLIENT
        /// <summary>
        /// Tries to get a stream property of the specified name.
        /// </summary>
        /// <param name="entityInstance">The instance of the entity to get the stream property for.</param>
        /// <param name="streamPropertyName">The stream property name to find.</param>
        /// <param name="streamProperty">The stream property found.</param>
        /// <returns>true if the stream property was found or if the stream property name was null (default stream).
        /// false if the stream property doesn't exist.</returns>
        internal static bool TryGetStreamProperty(IEdmStructuredValue entityInstance, string streamPropertyName, out IEdmProperty streamProperty)
        {
            Debug.Assert(entityInstance != null, "entityInstance != null");

            streamProperty = null;
            if (streamPropertyName != null)
            {
                streamProperty = entityInstance.Type.AsEntity().FindProperty(streamPropertyName);
                if (streamProperty == null)
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Gets the the CLR value for a primitive property.
        /// </summary>
        /// <param name="structuredValue">The structured value.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns>The clr value of the property.</returns>
        internal static object GetPrimitivePropertyClrValue(this IEdmStructuredValue structuredValue, string propertyName)
        {
            Debug.Assert(structuredValue != null, "entityInstance != null");
            IEdmStructuredTypeReference valueType = structuredValue.Type.AsStructured();

            IEdmPropertyValue propertyValue = structuredValue.FindPropertyValue(propertyName);
            if (propertyValue == null)
            {
                throw new ODataException(ErrorStrings.EdmValueUtils_PropertyDoesntExist(valueType.FullName(), propertyName));
            }

            if (propertyValue.Value.ValueKind == EdmValueKind.Null)
            {
                return null;
            }

            IEdmPrimitiveValue primitiveValue = propertyValue.Value as IEdmPrimitiveValue;
            if (primitiveValue == null)
            {
                throw new ODataException(ErrorStrings.EdmValueUtils_NonPrimitiveValue(propertyValue.Name, valueType.FullName()));
            }

            return primitiveValue.ToClrValue();
        }
#endif

        /// <summary>
        /// Converts a floating-point edm value to a clr value
        /// </summary>
        /// <param name="floatingValue">The edm floating-point value.</param>
        /// <param name="primitiveKind">Kind of the primitive.</param>
        /// <returns>The converted value</returns>
        private static object ConvertFloatingValue(IEdmFloatingValue floatingValue, EdmPrimitiveTypeKind primitiveKind)
        {
            Debug.Assert(floatingValue != null, "floatingValue != null");
            double doubleValue = floatingValue.Value;

            if (primitiveKind == EdmPrimitiveTypeKind.Single)
            {
                return Convert.ToSingle(doubleValue);
            }

            Debug.Assert(primitiveKind == EdmPrimitiveTypeKind.Double, "primitiveKind == EdmPrimitiveTypeKind.Double");
            return doubleValue;
        }

        /// <summary>
        /// Converts an integer edm value to a clr value.
        /// </summary>
        /// <param name="integerValue">The integer value.</param>
        /// <param name="primitiveKind">Kind of the primitive.</param>
        /// <returns>The converted value</returns>
        private static object ConvertIntegerValue(IEdmIntegerValue integerValue, EdmPrimitiveTypeKind primitiveKind)
        {
            Debug.Assert(integerValue != null, "integerValue != null");
            long longValue = integerValue.Value;

            switch (primitiveKind)
            {
                case EdmPrimitiveTypeKind.Int16:
                    return Convert.ToInt16(longValue);

                case EdmPrimitiveTypeKind.Int32:
                    return Convert.ToInt32(longValue);

                case EdmPrimitiveTypeKind.Byte:
                    return Convert.ToByte(longValue);

                case EdmPrimitiveTypeKind.SByte:
                    return Convert.ToSByte(longValue);

                default:
                    Debug.Assert(primitiveKind == EdmPrimitiveTypeKind.Int64, "primitiveKind == EdmPrimitiveTypeKind.Int64");
                    return longValue;
            }
        }


        /// <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);
        }

Microsoft.Data.Edm.Library.Values.EdmValue : IEdmValue, IEdmElement, IEdmDelayedValue

Methods :

public IEdmTypeReference get_Type()
public abstract EdmValueKind get_ValueKind()
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()