public static EnumType Create( string name, string namespaceName, PrimitiveType underlyingType, bool isFlags, IEnumerable <EnumMember> members, IEnumerable <MetadataProperty> metadataProperties) { Check.NotEmpty(name, nameof(name)); Check.NotEmpty(namespaceName, nameof(namespaceName)); Check.NotNull <PrimitiveType>(underlyingType, nameof(underlyingType)); if (!Helper.IsSupportedEnumUnderlyingType(underlyingType.PrimitiveTypeKind)) { throw new ArgumentException(Strings.InvalidEnumUnderlyingType, nameof(underlyingType)); } EnumType enumType = new EnumType(name, namespaceName, underlyingType, isFlags, DataSpace.CSpace); if (members != null) { foreach (EnumMember member in members) { if (!Helper.IsEnumMemberValueInRange(underlyingType.PrimitiveTypeKind, Convert.ToInt64(member.Value, (IFormatProvider)CultureInfo.InvariantCulture))) { throw new ArgumentException(Strings.EnumMemberValueOutOfItsUnderylingTypeRange(member.Value, (object)member.Name, (object)underlyingType.Name), nameof(members)); } enumType.AddMember(member); } } if (metadataProperties != null) { enumType.AddMetadataProperties(metadataProperties.ToList <MetadataProperty>()); } enumType.SetReadOnly(); return(enumType); }
// <summary> // Initializes a new instance of the EnumType class by using the specified <paramref name="name" />, // <paramref name="namespaceName" /> and <paramref name="isFlags" />. // </summary> // <param name="name"> The name of this enum type. </param> // <param name="namespaceName"> The namespace this enum type belongs to. </param> // <param name="underlyingType"> Underlying type of this enumeration type. </param> // <param name="isFlags"> Indicates whether the enum type is defined as flags (i.e. can be treated as a bit field). </param> // <param name="dataSpace"> DataSpace this enum type lives in. Can be either CSpace or OSpace </param> // <exception cref="System.ArgumentNullException">Thrown if name or namespace arguments are null</exception> // <remarks> // Note that enums live only in CSpace. // </remarks> internal EnumType(string name, string namespaceName, PrimitiveType underlyingType, bool isFlags, DataSpace dataSpace) : base(name, namespaceName, dataSpace) { DebugCheck.NotNull(underlyingType); Debug.Assert(Helper.IsSupportedEnumUnderlyingType(underlyingType.PrimitiveTypeKind), "Unsupported underlying type for enum."); Debug.Assert(dataSpace == DataSpace.CSpace || dataSpace == DataSpace.OSpace, "Enums can be only defined in CSpace or OSpace."); _isFlags = isFlags; _underlyingType = underlyingType; }
public static EnumType Create( string name, string namespaceName, PrimitiveType underlyingType, bool isFlags, IEnumerable <EnumMember> members, IEnumerable <MetadataProperty> metadataProperties) { Check.NotEmpty(name, "name"); Check.NotEmpty(namespaceName, "namespaceName"); Check.NotNull(underlyingType, "underlyingType"); if (!Helper.IsSupportedEnumUnderlyingType(underlyingType.PrimitiveTypeKind)) { throw new ArgumentException(Strings.InvalidEnumUnderlyingType, "underlyingType"); } var instance = new EnumType(name, namespaceName, underlyingType, isFlags, DataSpace.CSpace); if (members != null) { foreach (var member in members) { if (!Helper.IsEnumMemberValueInRange( underlyingType.PrimitiveTypeKind, Convert.ToInt64(member.Value, CultureInfo.InvariantCulture))) { throw new ArgumentException( Strings.EnumMemberValueOutOfItsUnderylingTypeRange( member.Value, member.Name, underlyingType.Name), "members"); } instance.AddMember(member); } } if (metadataProperties != null) { instance.AddMetadataProperties(metadataProperties); } instance.SetReadOnly(); return(instance); }
// <summary> // Initializes a new instance of the EnumType class from CLR enumeration type. // </summary> // <param name="clrType"> CLR enumeration type to create EnumType from. </param> // <remarks> // Note that this method expects that the <paramref name="clrType" /> is a valid CLR enum type // whose underlying type is a valid EDM primitive type. // Ideally this constructor should be protected and internal (Family and Assembly modifier) but // C# does not support this. In order to not expose this constructor to everyone internal is the // only option. // </remarks> internal EnumType(Type clrType) : base(clrType.Name, clrType.NestingNamespace() ?? string.Empty, DataSpace.OSpace) { DebugCheck.NotNull(clrType); Debug.Assert(clrType.IsEnum(), "enum type expected"); ClrProviderManifest.Instance.TryGetPrimitiveType(clrType.GetEnumUnderlyingType(), out _underlyingType); Debug.Assert(_underlyingType != null, "only primitive types expected here."); Debug.Assert( Helper.IsSupportedEnumUnderlyingType(_underlyingType.PrimitiveTypeKind), "unsupported CLR types should have been filtered out by .TryGetPrimitiveType() method."); _isFlags = clrType.GetCustomAttributes <FlagsAttribute>(inherit: false).Any(); foreach (var name in Enum.GetNames(clrType)) { AddMember( new EnumMember( name, Convert.ChangeType(Enum.Parse(clrType, name), clrType.GetEnumUnderlyingType(), CultureInfo.InvariantCulture))); } }