Exemplo 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)
            : this()
        {
            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 <>);
        }
Exemplo 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)
        {
            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;
            NullFieldOnError = false;

            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 <>);
        }
Exemplo n.º 3
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>
        /// <param name="defaultCultureName">Default culture name used for each properties if no converter is specified otherwise. If null, the default decimal separator (".") will be used.</param>
        internal FieldBase(FieldInfo fi, string defaultCultureName = null)
            : this()
        {
            FieldInfo = fi;
            FieldType = FieldInfo.FieldType;
            MemberInfo attibuteTarget = fi;

            FieldFriendlyName = AutoPropertyName(fi);
            if (string.IsNullOrEmpty(FieldFriendlyName) == false)
            {
                var prop = fi.DeclaringType.GetProperty(FieldFriendlyName);
                if (prop == null)
                {
                    FieldFriendlyName = null;
                }
                else
                {
                    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];
                Converter = conv.Converter;
                conv.ValidateTypes(FieldInfo);
            }
            else
            {
                Converter = ConvertHelpers.GetDefaultConverter(FieldFriendlyName ?? fi.Name,
                                                               FieldType,
                                                               defaultCultureName: defaultCultureName);
            }

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

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

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

                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 <>);
        }
Exemplo n.º 4
0
        public bool IsNull(object val)
        {
            if (val == null || val == DBNull.Value)
            {
                return(true);
            }
            // WARNING !!!
            // == comparison result does not match that of .Equals
            //bool BROKEN1 = NullValue != null && val == NullValue && val.GetType() == NullValue.GetType();
            //bool BROKEN2 = NullValue != null && NullValue == val && val.GetType() == NullValue.GetType();
            bool WORKS1 = NullValue != null && NullValue.Equals(val) && val.GetType() == NullValue.GetType();
            //bool WORKS2 = NullValue != null && val.Equals( NullValue ) && val.GetType() == NullValue.GetType();
            bool isSameValue = IsValueType && val.Equals(NullValue);

            return(WORKS1 || isSameValue);
        }
Exemplo n.º 5
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>
        protected FieldBase(FieldInfo fi, string delimiter)  : this()
        {
            FieldInfo = fi;
            FieldType = FieldInfo.FieldType;
            MemberInfo attibuteTarget = fi;

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

            FieldTypeInternal = FieldType.IsArray ? FieldType.GetElementType() : FieldType;

            IsStringField = FieldTypeInternal == typeof(string);

            var attributes = attibuteTarget.GetCustomAttributes(typeof(FieldConverterAttribute), true);

            foreach (var attribute in attributes)
            {
                var fc = attribute as FieldConverterAttribute;
                if (fc != null)
                {
                    fc.Delimiter = delimiter;
                }
#if DEBUG
                var del = attribute as DelimitedRecordAttribute;
                if (del != null)
                {
                    Debug.Assert(del.Delimiter == delimiter);
                }
#endif
            }
            if (attributes.Length > 0)
            {
                var conv = (FieldConverterAttribute)attributes[0];
                Converter = conv.Converter;
                conv.ValidateTypes(FieldInfo);
            }
            else
            {
                Converter = ConverterFactory.GetDefaultConverter(FieldFriendlyName ?? fi.Name, FieldType);
            }

            if (Converter != null)
            {
                if (Converter.Type == null)
                {
                    throw new ArgumentNullException(nameof(IFieldConverter.Type), $"Converter type [{Converter.GetType().Name}] can not be null!");
                }
                if (Converter.Type != FieldTypeInternal)
                {
                    throw new TypeMismatchException(Converter.Type, $"{Converter.GetType().Name} destination type {Converter.Type} != expected type {FieldTypeInternal}");
                }
                //Converter.mDestinationType = FieldTypeInternal;
            }

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

            if (attributes.Length > 0)
            {
                NullValue = ((FieldNullValueAttribute)attributes[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} which is not asignable to the field {FieldInfo.Name} Type: {FieldTypeInternal.Name}");
                    }
                }
            }

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