/// <summary> /// Registers an enum type as part of the model and returns an object that can be used to configure the enum type. /// </summary> /// <param name="clrType">The type to be registered or configured.</param> /// <returns>The configuration object for the specified enum type.</returns> public virtual EnumTypeConfiguration AddEnumType(Type clrType) { if (clrType == null) { throw new ArgumentNullException(nameof(clrType)); } if (!TypeHelper.IsEnum(clrType)) { throw Error.Argument("type", SRResources.TypeCannotBeEnum, clrType.FullName); } if (!_enumTypes.ContainsKey(clrType)) { EnumTypeConfiguration enumTypeConfig = new EnumTypeConfiguration(this, clrType); _enumTypes.Add(clrType, enumTypeConfig); return(enumTypeConfig); } else { EnumTypeConfiguration enumTypeConfig = _enumTypes[clrType]; if (enumTypeConfig.ClrType != clrType) { throw Error.Argument("type", SRResources.TypeCannotBeEnum, clrType.FullName); } return(enumTypeConfig); } }
/// <inheritdoc /> public override EnumTypeConfiguration AddEnumType(Type type) { if (type == null) { throw Error.ArgumentNull("type"); } if (!TypeHelper.IsEnum(type)) { throw Error.Argument("type", SRResources.TypeCannotBeEnum, type.FullName); } EnumTypeConfiguration enumTypeConfiguration = EnumTypes.SingleOrDefault(e => e.ClrType == type); if (enumTypeConfiguration == null) { enumTypeConfiguration = base.AddEnumType(type); foreach (object member in Enum.GetValues(type)) { bool addedExplicitly = enumTypeConfiguration.Members.Any(m => m.Name.Equals(member.ToString(), StringComparison.Ordinal)); EnumMemberConfiguration enumMemberConfiguration = enumTypeConfiguration.AddMember((Enum)member); enumMemberConfiguration.AddedExplicitly = addedExplicitly; } ApplyEnumTypeConventions(enumTypeConfiguration); } return(enumTypeConfiguration); }
/// <summary> /// Initializes a new instance of the <see cref="EnumMemberConfiguration"/> class. /// </summary> /// <param name="member">The member of the enum type.</param> /// <param name="declaringType">The declaring type of the member.</param> public EnumMemberConfiguration(Enum member, EnumTypeConfiguration declaringType) { MemberInfo = member ?? throw new ArgumentNullException(nameof(member)); DeclaringType = declaringType ?? throw new ArgumentNullException(nameof(declaringType)); Contract.Assert(member.GetType() == declaringType.ClrType); AddedExplicitly = true; _name = Enum.GetName(member.GetType(), member); }
internal NullableEnumTypeConfiguration(EnumTypeConfiguration enumTypeConfiguration) { this.ClrType = typeof(Nullable <>).MakeGenericType(enumTypeConfiguration.ClrType); this.FullName = enumTypeConfiguration.FullName; this.Namespace = enumTypeConfiguration.Namespace; this.Name = enumTypeConfiguration.Name; this.Kind = enumTypeConfiguration.Kind; this.ModelBuilder = enumTypeConfiguration.ModelBuilder; this.EnumTypeConfiguration = enumTypeConfiguration; }
private IEdmTypeConfiguration GetOperationTypeConfiguration(Type clrType) { Type type = TypeHelper.GetUnderlyingTypeOrSelf(clrType); IEdmTypeConfiguration edmTypeConfiguration; if (TypeHelper.IsEnum(type)) { edmTypeConfiguration = ModelBuilder.GetTypeConfigurationOrNull(type); if (edmTypeConfiguration != null && TypeHelper.IsNullable(clrType)) { edmTypeConfiguration = ((EnumTypeConfiguration)edmTypeConfiguration).GetNullableEnumTypeConfiguration(); } } else { edmTypeConfiguration = ModelBuilder.GetTypeConfigurationOrNull(clrType); } if (edmTypeConfiguration == null) { if (TypeHelper.IsEnum(type)) { EnumTypeConfiguration enumTypeConfiguration = ModelBuilder.AddEnumType(type); if (TypeHelper.IsNullable(clrType)) { edmTypeConfiguration = enumTypeConfiguration.GetNullableEnumTypeConfiguration(); } else { edmTypeConfiguration = enumTypeConfiguration; } } else { edmTypeConfiguration = ModelBuilder.AddComplexType(clrType); } } return(edmTypeConfiguration); }
private void ApplyEnumTypeConventions(EnumTypeConfiguration enumTypeConfiguration) { DataContractAttributeEnumTypeConvention typeConvention = new DataContractAttributeEnumTypeConvention(); typeConvention.Apply(enumTypeConfiguration, this); }
internal EnumTypeConfiguration(EnumTypeConfiguration configuration) { Contract.Assert(configuration != null); Contract.Assert(configuration.ClrType == typeof(TEnumType)); _configuration = configuration; }