Ejemplo n.º 1
0
        /// <summary>
        /// Create a field base from a fieldinfo object
        /// Verify the settings against the actual field to ensure it will work.
        /// </summary>
        /// <param name="fi">Field Info Object</param>
        internal FieldBase(FieldInfo fi)
        {
            IsNullableType = false;
            TrimMode       = TrimMode.None;
            FieldOrder     = null;
            InNewLine      = false;
            NextIsOptional = false;
            IsOptional     = false;
            TrimChars      = null;
            NullValue      = null;
            TrailingArray  = false;
            IsLast         = false;
            IsFirst        = false;
            IsArray        = false;
            CharsToDiscard = 0;
            FieldInfo      = fi;
            FieldType      = FieldInfo.FieldType;

            if (FieldType.IsArray)
            {
                FieldTypeInternal = FieldType.GetElementType();
            }
            else
            {
                FieldTypeInternal = FieldType;
            }

            IsStringField = FieldTypeInternal == typeof(string);

            object[] attribs = fi.GetCustomAttributes(typeof(FieldConverterAttribute), true);

            if (attribs.Length > 0)
            {
                var conv = (FieldConverterAttribute)attribs[0];
                this.Converter = conv.Converter;
                conv.ValidateTypes(FieldInfo);
            }
            else
            {
                this.Converter = ConvertHelpers.GetDefaultConverter(fi.Name, FieldType);
            }

            if (this.Converter != null)
            {
                this.Converter.mDestinationType = FieldTypeInternal;
            }

            attribs = fi.GetCustomAttributes(typeof(FieldNullValueAttribute), true);

            if (attribs.Length > 0)
            {
                NullValue = ((FieldNullValueAttribute)attribs[0]).NullValue;
                //				mNullValueOnWrite = ((FieldNullValueAttribute) attribs[0]).NullValueOnWrite;

                if (NullValue != null)
                {
                    if (!FieldTypeInternal.IsAssignableFrom(NullValue.GetType()))
                    {
                        throw new BadUsageException("The NullValue is of type: " + NullValue.GetType().Name +
                                                    " that is not asignable to the field " + FieldInfo.Name + " of type: " +
                                                    FieldTypeInternal.Name);
                    }
                }
            }

            IsNullableType = FieldTypeInternal.IsValueType &&
                             FieldTypeInternal.IsGenericType &&
                             FieldTypeInternal.GetGenericTypeDefinition() == typeof(Nullable <>);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create a field base from a fieldinfo object
        /// Verify the settings against the actual field to ensure it will work.
        /// </summary>
        /// <param name="fi">Field Info Object</param>
        internal FieldBase(FieldInfo fi)
            : this()
        {
            FieldInfo = fi;
            FieldType = FieldInfo.FieldType;
            MemberInfo attibuteTarget = fi;

            this.FieldFriendlyName = AutoPropertyName(fi);
            if (string.IsNullOrEmpty(FieldFriendlyName) == false)
            {
                var prop = fi.DeclaringType.GetProperty(this.FieldFriendlyName);
                if (prop == null)
                {
                    this.FieldFriendlyName = null;
                }
                else
                {
                    this.IsAutoProperty = true;
                    attibuteTarget      = prop;
                }
            }

            if (FieldType.IsArray)
            {
                FieldTypeInternal = FieldType.GetElementType();
            }
            else
            {
                FieldTypeInternal = FieldType;
            }

            IsStringField = FieldTypeInternal == typeof(string);

            object[] attribs = attibuteTarget.GetCustomAttributes(typeof(FieldConverterAttribute), true);

            if (attribs.Length > 0)
            {
                var conv = (FieldConverterAttribute)attribs[0];
                this.Converter = conv.Converter;
                conv.ValidateTypes(FieldInfo);
            }
            else
            {
                this.Converter = ConvertHelpers.GetDefaultConverter(FieldFriendlyName ?? fi.Name, FieldType);
            }

            if (this.Converter != null)
            {
                this.Converter.mDestinationType = FieldTypeInternal;
            }

            attribs = attibuteTarget.GetCustomAttributes(typeof(FieldNullValueAttribute), true);

            if (attribs.Length > 0)
            {
                NullValue = ((FieldNullValueAttribute)attribs[0]).NullValue;
                //				mNullValueOnWrite = ((FieldNullValueAttribute) attribs[0]).NullValueOnWrite;

                if (NullValue != null)
                {
                    if (!FieldTypeInternal.IsAssignableFrom(NullValue.GetType()))
                    {
                        throw new BadUsageException("The NullValue is of type: " + NullValue.GetType().Name +
                                                    " that is not asignable to the field " + FieldInfo.Name +
                                                    " of type: " +
                                                    FieldTypeInternal.Name);
                    }
                }
            }

            IsNullableType = FieldTypeInternal.IsValueType &&
                             FieldTypeInternal.IsGenericType &&
                             FieldTypeInternal.GetGenericTypeDefinition() == typeof(Nullable <>);
        }