/// <inheritdoc /> public override EnumTypeConfiguration AddEnumType(Type type) { if (type == null) { throw Error.ArgumentNull("type"); } if (!type.GetTypeInfo().IsEnum) { 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)) { enumTypeConfiguration.AddMember((Enum)member); } } return(enumTypeConfiguration); }
/// <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())); EnumMemberConfiguration enumMemberConfiguration = enumTypeConfiguration.AddMember((Enum)member); enumMemberConfiguration.AddedExplicitly = addedExplicitly; } ApplyEnumTypeConventions(enumTypeConfiguration); } return(enumTypeConfiguration); }
/// <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="type">The type to be registered or configured.</param> /// <returns>The configuration object for the specified enum type.</returns> public virtual EnumTypeConfiguration AddEnumType(Type type) { if (type == null) { throw Error.ArgumentNull("type"); } if (!TypeHelper.IsEnum(type)) { throw Error.Argument("type", SRResources.TypeCannotBeEnum, type.FullName); } if (!_enumTypes.ContainsKey(type)) { EnumTypeConfiguration enumTypeConfig = new EnumTypeConfiguration(this, type); _enumTypes.Add(type, enumTypeConfig); return(enumTypeConfig); } else { EnumTypeConfiguration enumTypeConfig = _enumTypes[type]; if (enumTypeConfig.ClrType != type) { throw Error.Argument("type", SRResources.TypeCannotBeEnum, type.FullName); } return(enumTypeConfig); } }
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; }
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; }
/// <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) { if (member == null) { throw Error.ArgumentNull("member"); } if (declaringType == null) { throw Error.ArgumentNull("declaringType"); } Contract.Assert(member.GetType() == declaringType.ClrType); MemberInfo = member; DeclaringType = declaringType; AddedExplicitly = true; _name = Enum.GetName(member.GetType(), member); }
private IEdmTypeConfiguration GetProcedureTypeConfiguration(Type clrType) { Type type = TypeHelper.GetUnderlyingTypeOrSelf(clrType); IEdmTypeConfiguration edmTypeConfiguration; if (type.GetTypeInfo().IsEnum) { edmTypeConfiguration = ModelBuilder.GetTypeConfigurationOrNull(type); if (edmTypeConfiguration != null && EdmLibHelpers.IsNullable(clrType)) { edmTypeConfiguration = ((EnumTypeConfiguration)edmTypeConfiguration).GetNullableEnumTypeConfiguration(); } } else { edmTypeConfiguration = ModelBuilder.GetTypeConfigurationOrNull(clrType); } if (edmTypeConfiguration == null) { if (type.GetTypeInfo().IsEnum) { EnumTypeConfiguration enumTypeConfiguration = ModelBuilder.AddEnumType(type); if (EdmLibHelpers.IsNullable(clrType)) { edmTypeConfiguration = enumTypeConfiguration.GetNullableEnumTypeConfiguration(); } else { edmTypeConfiguration = enumTypeConfiguration; } } else { edmTypeConfiguration = ModelBuilder.AddComplexType(clrType); } } return(edmTypeConfiguration); }
private void CreateEnumTypeBody(EdmEnumType type, EnumTypeConfiguration config) { Contract.Assert(type != null); Contract.Assert(config != null); foreach (EnumMemberConfiguration member in config.Members) { // EdmIntegerConstant can only support a value of long type. long value; try { value = Convert.ToInt64(member.MemberInfo, CultureInfo.InvariantCulture); } catch { throw Error.Argument("value", SRResources.EnumValueCannotBeLong, Enum.GetName(member.MemberInfo.GetType(), member.MemberInfo)); } EdmEnumMember edmMember = new EdmEnumMember(type, member.Name, new EdmEnumMemberValue(value)); type.AddMember(edmMember); _members[member.MemberInfo] = edmMember; } }
private void ApplyEnumTypeConventions(EnumTypeConfiguration enumTypeConfiguration) { DataContractAttributeEnumTypeConvention typeConvention = new DataContractAttributeEnumTypeConvention(); typeConvention.Apply(enumTypeConfiguration, this); }
private void CreateEdmTypeHeader(IEdmTypeConfiguration config) { IEdmType edmType = GetEdmType(config.ClrType); if (edmType == null) { if (config.Kind == EdmTypeKind.Complex) { ComplexTypeConfiguration complex = (ComplexTypeConfiguration)config; IEdmComplexType baseType = null; if (complex.BaseType != null) { CreateEdmTypeHeader(complex.BaseType); baseType = GetEdmType(complex.BaseType.ClrType) as IEdmComplexType; Contract.Assert(baseType != null); } EdmComplexType complexType = new EdmComplexType(config.Namespace, config.Name, baseType, complex.IsAbstract ?? false, complex.IsOpen); _types.Add(config.ClrType, complexType); if (complex.IsOpen) { // add a mapping between the open complex type and its dynamic property dictionary. _openTypes.Add(complexType, complex.DynamicPropertyDictionary); } edmType = complexType; } else if (config.Kind == EdmTypeKind.Entity) { EntityTypeConfiguration entity = config as EntityTypeConfiguration; Contract.Assert(entity != null); IEdmEntityType baseType = null; if (entity.BaseType != null) { CreateEdmTypeHeader(entity.BaseType); baseType = GetEdmType(entity.BaseType.ClrType) as IEdmEntityType; Contract.Assert(baseType != null); } EdmEntityType entityType = new EdmEntityType(config.Namespace, config.Name, baseType, entity.IsAbstract ?? false, entity.IsOpen, entity.HasStream); _types.Add(config.ClrType, entityType); if (entity.IsOpen) { // add a mapping between the open entity type and its dynamic property dictionary. _openTypes.Add(entityType, entity.DynamicPropertyDictionary); } edmType = entityType; } else { EnumTypeConfiguration enumTypeConfiguration = config as EnumTypeConfiguration; // The config has to be enum. Contract.Assert(enumTypeConfiguration != null); _types.Add(enumTypeConfiguration.ClrType, new EdmEnumType(enumTypeConfiguration.Namespace, enumTypeConfiguration.Name, GetTypeKind(enumTypeConfiguration.UnderlyingType), enumTypeConfiguration.IsFlags)); } } IEdmStructuredType structuredType = edmType as IEdmStructuredType; StructuralTypeConfiguration structuralTypeConfiguration = config as StructuralTypeConfiguration; if (structuredType != null && structuralTypeConfiguration != null && !_structuredTypeQuerySettings.ContainsKey(structuredType)) { ModelBoundQuerySettings querySettings = structuralTypeConfiguration.QueryConfiguration.ModelBoundQuerySettings; if (querySettings != null) { _structuredTypeQuerySettings.Add(structuredType, structuralTypeConfiguration.QueryConfiguration.ModelBoundQuerySettings); } } }
internal EnumTypeConfiguration(EnumTypeConfiguration configuration) { Contract.Assert(configuration != null); Contract.Assert(configuration.ClrType == typeof(TEnumType)); _configuration = configuration; }
private void CreateEdmTypeHeader(IEdmTypeConfiguration config) { if (GetEdmType(config.ClrType) == null) { if (config.Kind == EdmTypeKind.Complex) { ComplexTypeConfiguration complex = (ComplexTypeConfiguration)config; IEdmComplexType baseType = null; if (complex.BaseType != null) { CreateEdmTypeHeader(complex.BaseType); baseType = GetEdmType(complex.BaseType.ClrType) as IEdmComplexType; Contract.Assert(baseType != null); } EdmComplexType complexType = new EdmComplexType(config.Namespace, config.Name, baseType, complex.IsAbstract ?? false, complex.IsOpen); _types.Add(config.ClrType, complexType); if (complex.IsOpen) { // add a mapping between the open complex type and its dynamic property dictionary. _openTypes.Add(complexType, complex.DynamicPropertyDictionary); } } else if (config.Kind == EdmTypeKind.Entity) { EntityTypeConfiguration entity = config as EntityTypeConfiguration; Contract.Assert(entity != null); IEdmEntityType baseType = null; if (entity.BaseType != null) { CreateEdmTypeHeader(entity.BaseType); baseType = GetEdmType(entity.BaseType.ClrType) as IEdmEntityType; Contract.Assert(baseType != null); } EdmEntityType entityType = new EdmEntityType(config.Namespace, config.Name, baseType, entity.IsAbstract ?? false, entity.IsOpen); _types.Add(config.ClrType, entityType); if (entity.IsOpen) { // add a mapping between the open entity type and its dynamic property dictionary. _openTypes.Add(entityType, entity.DynamicPropertyDictionary); } } else { EnumTypeConfiguration enumTypeConfiguration = config as EnumTypeConfiguration; // The config has to be enum. Contract.Assert(enumTypeConfiguration != null); _types.Add(enumTypeConfiguration.ClrType, new EdmEnumType(enumTypeConfiguration.Namespace, enumTypeConfiguration.Name, GetTypeKind(enumTypeConfiguration.UnderlyingType), enumTypeConfiguration.IsFlags)); } } }
private void CreateEnumTypeBody(EdmEnumType type, EnumTypeConfiguration config) { Contract.Assert(type != null); Contract.Assert(config != null); foreach (EnumMemberConfiguration member in config.Members) { // EdmIntegerConstant can only support a value of long type. long value; try { value = Convert.ToInt64(member.MemberInfo, CultureInfo.InvariantCulture); } catch { throw Error.Argument("value", SRResources.EnumValueCannotBeLong, Enum.GetName(member.MemberInfo.GetType(), member.MemberInfo)); } EdmEnumMember edmMember = new EdmEnumMember(type, member.Name, new EdmIntegerConstant(value)); type.AddMember(edmMember); _members[member.MemberInfo] = edmMember; } }