internal static Facet Create(
            FacetDescription facetDescription,
            object value,
            bool bypassKnownValues)
        {
            if (!bypassKnownValues)
            {
                if (object.ReferenceEquals(value, (object)null))
                {
                    return(facetDescription.NullValueFacet);
                }
                if (object.Equals(facetDescription.DefaultValue, value))
                {
                    return(facetDescription.DefaultValueFacet);
                }
                if (facetDescription.FacetType.Identity == "Edm.Boolean")
                {
                    bool flag = (bool)value;
                    return(facetDescription.GetBooleanFacet(flag));
                }
            }
            Facet facet = new Facet(facetDescription, value);

            if (value != null && !Helper.IsUnboundedFacetValue(facet) && (!Helper.IsVariableFacetValue(facet) && facet.FacetType.ClrType != (Type)null))
            {
                value.GetType();
            }
            return(facet);
        }
Example #2
0
        /// <summary>
        ///     Creates a Facet instance with the specified value for the given
        ///     facet description.
        /// </summary>
        /// <param name="facetDescription"> The object describing this facet </param>
        /// <param name="value"> The value of the facet </param>
        /// <param name="bypassKnownValues"> true to bypass caching and known values; false otherwise. </param>
        /// <exception cref="System.ArgumentNullException">Thrown if facetDescription argument is null</exception>
        internal static Facet Create(FacetDescription facetDescription, object value, bool bypassKnownValues)
        {
            DebugCheck.NotNull(facetDescription);

            if (!bypassKnownValues)
            {
                // Reuse facets with a null value.
                if (ReferenceEquals(value, null))
                {
                    return(facetDescription.NullValueFacet);
                }

                // Reuse facets with a default value.
                if (Equals(facetDescription.DefaultValue, value))
                {
                    return(facetDescription.DefaultValueFacet);
                }

                // Special case boolean facets.
                if (facetDescription.FacetType.Identity == "Edm.Boolean")
                {
                    var boolValue = (bool)value;
                    return(facetDescription.GetBooleanFacet(boolValue));
                }
            }

            var result = new Facet(facetDescription, value);

            // Check the type of the value only if we know what the correct CLR type is
            if (value != null &&
                !Helper.IsUnboundedFacetValue(result) &&
                !Helper.IsVariableFacetValue(result) &&
                result.FacetType.ClrType != null)
            {
                var valueType = value.GetType();
                Debug.Assert(
                    valueType == result.FacetType.ClrType ||
                    result.FacetType.ClrType.IsAssignableFrom(valueType),
                    string.Format(
                        CultureInfo.CurrentCulture, "The facet {0} has type {1}, but a value of type {2} was supplied.", result.Name,
                        result.FacetType.ClrType, valueType)
                    );
            }

            return(result);
        }