internal static void ValidateConstant(TypeUsage constantType, object value) { ArgumentValidation.CheckType(constantType, nameof(constantType)); EnumType type1; if (TypeHelpers.TryGetEdmType <EnumType>(constantType, out type1)) { Type clrEquivalentType = type1.UnderlyingType.ClrEquivalentType; if (clrEquivalentType != value.GetType() && (!value.GetType().IsEnum() || !ArgumentValidation.ClrEdmEnumTypesMatch(type1, value.GetType()))) { throw new ArgumentException(Strings.Cqt_Constant_ClrEnumTypeDoesNotMatchEdmEnumType((object)value.GetType().Name, (object)type1.Name, (object)clrEquivalentType.Name), nameof(value)); } } else { PrimitiveType type2; if (!TypeHelpers.TryGetEdmType <PrimitiveType>(constantType, out type2)) { throw new ArgumentException(Strings.Cqt_Constant_InvalidConstantType((object)constantType.ToString()), nameof(constantType)); } PrimitiveTypeKind primitiveTypeKind; if ((!ArgumentValidation.TryGetPrimitiveTypeKind(value.GetType(), out primitiveTypeKind) || type2.PrimitiveTypeKind != primitiveTypeKind) && (!Helper.IsGeographicType(type2) || primitiveTypeKind != PrimitiveTypeKind.Geography) && (!Helper.IsGeometricType(type2) || primitiveTypeKind != PrimitiveTypeKind.Geometry)) { throw new ArgumentException(Strings.Cqt_Constant_InvalidValueForType((object)constantType.ToString()), nameof(value)); } } }
internal static TypeUsage ValidateConstant(Type type) { PrimitiveTypeKind primitiveTypeKind; if (!ArgumentValidation.TryGetPrimitiveTypeKind(type, out primitiveTypeKind)) { throw new ArgumentException(Strings.Cqt_Constant_InvalidType, nameof(type)); } return(TypeHelpers.GetLiteralTypeUsage(primitiveTypeKind)); }
private static bool ClrEdmEnumTypesMatch(EnumType edmEnumType, Type clrEnumType) { PrimitiveTypeKind primitiveTypeKind; if (clrEnumType.Name != edmEnumType.Name || clrEnumType.GetEnumNames().Length < edmEnumType.Members.Count || (!ArgumentValidation.TryGetPrimitiveTypeKind(clrEnumType.GetEnumUnderlyingType(), out primitiveTypeKind) || primitiveTypeKind != edmEnumType.UnderlyingType.PrimitiveTypeKind)) { return(false); } foreach (EnumMember member in edmEnumType.Members) { if (!((IEnumerable <string>)clrEnumType.GetEnumNames()).Contains <string>(member.Name) || !member.Value.Equals(Convert.ChangeType(Enum.Parse(clrEnumType, member.Name), clrEnumType.GetEnumUnderlyingType(), (IFormatProvider)CultureInfo.InvariantCulture))) { return(false); } } return(true); }