コード例 #1
0
        /// <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 (!type.IsEnum)
            {
                throw Error.Argument("type", SRResources.TypeCannotBeEnum, type.FullName);
            }

            if (!this._enumTypes.ContainsKey(type))
            {
                EnumTypeConfiguration enumTypeConfig = new EnumTypeConfiguration(this, type);
                this._enumTypes.Add(type, enumTypeConfig);
                return(enumTypeConfig);
            }
            else
            {
                EnumTypeConfiguration enumTypeConfig = this._enumTypes[type];
                if (enumTypeConfig.ClrType != type)
                {
                    throw Error.Argument("type", SRResources.TypeCannotBeEnum, type.FullName);
                }

                return(enumTypeConfig);
            }
        }
コード例 #2
0
 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;
 }
コード例 #3
0
        /// <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);

            this.MemberInfo      = member;
            this.DeclaringType   = declaringType;
            this.AddedExplicitly = true;
            this._name           = Enum.GetName(member.GetType(), member);
        }
コード例 #4
0
        private IEdmTypeConfiguration GetOperationTypeConfiguration(Type clrType)
        {
            Type type = TypeHelper.GetUnderlyingTypeOrSelf(clrType);
            IEdmTypeConfiguration edmTypeConfiguration;

            if (type.IsEnum)
            {
                edmTypeConfiguration = this.ModelBuilder.GetTypeConfigurationOrNull(type);

                if (edmTypeConfiguration != null && EdmLibHelpers.IsNullable(clrType))
                {
                    edmTypeConfiguration = ((EnumTypeConfiguration)edmTypeConfiguration).GetNullableEnumTypeConfiguration();
                }
            }
            else
            {
                edmTypeConfiguration = this.ModelBuilder.GetTypeConfigurationOrNull(clrType);
            }

            if (edmTypeConfiguration == null)
            {
                if (type.IsEnum)
                {
                    EnumTypeConfiguration enumTypeConfiguration = this.ModelBuilder.AddEnumType(type);

                    if (EdmLibHelpers.IsNullable(clrType))
                    {
                        edmTypeConfiguration = enumTypeConfiguration.GetNullableEnumTypeConfiguration();
                    }
                    else
                    {
                        edmTypeConfiguration = enumTypeConfiguration;
                    }
                }
                else
                {
                    edmTypeConfiguration = this.ModelBuilder.AddComplexType(clrType);
                }
            }

            return(edmTypeConfiguration);
        }
コード例 #5
0
        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);
                this._members[member.MemberInfo] = edmMember;
            }
        }
 internal EnumTypeConfiguration(EnumTypeConfiguration configuration)
 {
     Contract.Assert(configuration != null);
     Contract.Assert(configuration.ClrType == typeof(TEnumType));
     this._configuration = configuration;
 }
コード例 #7
0
        private void CreateEdmTypeHeader(IEdmTypeConfiguration config)
        {
            IEdmType edmType = this.GetEdmType(config.ClrType);

            if (edmType == null)
            {
                if (config.Kind == EdmTypeKind.Complex)
                {
                    ComplexTypeConfiguration complex  = (ComplexTypeConfiguration)config;
                    IEdmComplexType          baseType = null;
                    if (complex.BaseType != null)
                    {
                        this.CreateEdmTypeHeader(complex.BaseType);
                        baseType = this.GetEdmType(complex.BaseType.ClrType) as IEdmComplexType;

                        Contract.Assert(baseType != null);
                    }

                    EdmComplexType complexType = new EdmComplexType(config.Namespace, config.Name,
                                                                    baseType, complex.IsAbstract ?? false, complex.IsOpen);

                    this._types.Add(config.ClrType, complexType);

                    if (complex.IsOpen)
                    {
                        // add a mapping between the open complex type and its dynamic property dictionary.
                        this._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)
                    {
                        this.CreateEdmTypeHeader(entity.BaseType);
                        baseType = this.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);
                    this._types.Add(config.ClrType, entityType);

                    if (entity.IsOpen)
                    {
                        // add a mapping between the open entity type and its dynamic property dictionary.
                        this._openTypes.Add(entityType, entity.DynamicPropertyDictionary);
                    }
                    edmType = entityType;
                }
                else
                {
                    EnumTypeConfiguration enumTypeConfiguration = config as EnumTypeConfiguration;

                    // The config has to be enum.
                    Contract.Assert(enumTypeConfiguration != null);

                    this._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 &&
                !this._structuredTypeQuerySettings.ContainsKey(structuredType))
            {
                ModelBoundQuerySettings querySettings =
                    structuralTypeConfiguration.QueryConfiguration.ModelBoundQuerySettings;
                if (querySettings != null)
                {
                    this._structuredTypeQuerySettings.Add(structuredType,
                                                          structuralTypeConfiguration.QueryConfiguration.ModelBoundQuerySettings);
                }
            }
        }