예제 #1
0
        private void CreateConverter(Type convType, object[] args)
        {
            if (typeof(ConverterBase).IsAssignableFrom(convType))
            {
                ConstructorInfo constructor;
                constructor = convType.GetConstructor(ArgsToTypes(args));

                if (constructor == null)
                {
                    throw new BadUsageException("Constructor with " + args.Length.ToString() + " arguments of type " + convType.Name + " not found !!");
                }

                try
                {
                    mConverter = (ConverterBase)constructor.Invoke(args);
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
            }
#if !MINI
            else if (convType.IsEnum)
            {
                mConverter = new EnumConverter(convType);
            }
#endif
            else
            {
                throw new BadUsageException("The custom converter must inherit from ConverterBase");
            }
        }
예제 #2
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);
                }
            }

            mIsNullableType = mFieldTypeInternal.IsValueType &&
                                    mFieldTypeInternal.IsGenericType &&
                                    mFieldTypeInternal.GetGenericTypeDefinition() == typeof(Nullable<>);
        }
        private void CreateConverter(Type convType, object[] args)
        {
            if (typeof(ConverterBase).IsAssignableFrom(convType))
            {
                ConstructorInfo constructor;
                constructor = convType.GetConstructor(
                    BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic,
                    null,
                    ArgsToTypes(args),
                    null);

                if (constructor == null)
                {
                    if (args.Length == 0)
                    {
                        throw new BadUsageException("Empty constructor for converter: " + convType.Name + " was not found. You must add a constructor without args (can be public or private)");
                    }
                    else
                    {
                        throw new BadUsageException("Constructor for converter: " + convType.Name + " with these arguments: (" + ArgsDesc(args) + ") was not found. You must add a constructor with this signature (can be public or private)");
                    }
                }

                try
                {
                    Converter = (ConverterBase)constructor.Invoke(args);
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
            }
#if !MINI
            else if (convType.IsEnum)
            {
                Converter = new EnumConverter(convType);
            }
#endif
            else
            {
                throw new BadUsageException("The custom converter must inherit from ConverterBase");
            }
        }
        private void CreateConverter(Type convType, object[] args)
        {
            if (typeof (ConverterBase).IsAssignableFrom(convType))
            {
                ConstructorInfo constructor;
                constructor = convType.GetConstructor(
                                        BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic,
                                        null,
                                        ArgsToTypes(args),
                                        null);

                if (constructor == null)
                {
                    if (args.Length == 0)
                        throw new BadUsageException("Empty constructor for converter: " + convType.Name + " was not found. You must add a constructor without args (can be public or private)");
                    else
                        throw new BadUsageException("Constructor for converter: " + convType.Name + " with these arguments: (" + ArgsDesc(args) + ") was not found. You must add a constructor with this signature (can be public or private)");
                }

                try
                {
                    Converter = (ConverterBase) constructor.Invoke(args);
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }

            }
            #if ! MINI
            else if (convType.IsEnum)
            {
                Converter = new EnumConverter(convType);
            }
            #endif
            else
                throw new BadUsageException("The custom converter must inherit from ConverterBase");
        }