コード例 #1
0
        internal FieldBase(FieldInfo fi)
        {
            mFieldInfo = fi;
            mFieldType = mFieldInfo.FieldType;

            if (mFieldType.IsArray)
            {
                mFieldTypeInternal = mFieldType.GetElementType();
            }
            else
            {
                mFieldTypeInternal = mFieldType;
            }

            mIsStringField = mFieldTypeInternal == strType;

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

            if (attribs.Length > 0)
            {
                FieldConverterAttribute conv = (FieldConverterAttribute)attribs[0];
                mConvertProvider = conv.Converter;
                conv.ValidateTypes(mFieldInfo);
            }
            else
            {
                mConvertProvider = ConvertHelpers.GetDefaultConverter(fi.Name, mFieldType);
            }

            if (mConvertProvider != null)
            {
                mConvertProvider.mDestinationType = mFieldTypeInternal;
            }

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

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

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


#if NET_2_0
            mIsNullableType = mFieldTypeInternal.IsValueType &&
                              mFieldTypeInternal.IsGenericType &&
                              mFieldTypeInternal.GetGenericTypeDefinition() == typeof(Nullable <>);
#endif
        }
コード例 #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)
        {
            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)
            {
                FieldConverterAttribute conv = (FieldConverterAttribute)attribs[0];
                ConvertProvider = conv.Converter;
                conv.ValidateTypes(FieldInfo);
            }
            else
            {
                ConvertProvider = ConvertHelpers.GetDefaultConverter(fi.Name, FieldType);
            }

            if (ConvertProvider != null)
            {
                ConvertProvider.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 <>);
        }