예제 #1
0
        private PropertyMeta.ClassType BuildClassType(Type propertyClass, DtoAttribute annotation)
        {
            PropertyMeta.ClassType propertyClassType = PropertyMeta.ClassType.Value;
            if (typeof(string).IsAssignableFrom(propertyClass))
            {
                propertyClassType = PropertyMeta.ClassType.Value;
            }
            else if (typeof(Dtobase).IsAssignableFrom(propertyClass))
            {
                propertyClassType = annotation.IsCustomType ? PropertyMeta.ClassType.CustomType : PropertyMeta.ClassType.Class;
            }
            else if (typeof(IEnumerable).IsAssignableFrom(propertyClass))
            {
                propertyClassType = annotation.IsCustomType ? PropertyMeta.ClassType.CustomTypeCollection : PropertyMeta.ClassType.Collection;
            }

            return(propertyClassType);
        }
예제 #2
0
        /// <summary>
        /// Creates a property parser instance based on property class
        /// </summary>
        /// <param name="name">property name</param>
        /// <param name="propertyClass">property class</param>
        /// <param name="propertyClassType">property class type</param>
        /// <param name="annotation">DtoAttribute instance</param>
        /// <returns>parser instance</returns>
        public IParser BuildParser(string name, Type propertyClass, PropertyMeta.ClassType propertyClassType, DtoAttribute annotation)
        {
            if (PropertyMeta.ClassType.Value != propertyClassType)
            {
                return(null);
            }

            IParser parser = null;

            try
            {
                if (null != annotation.Parser)
                {
                    parser = (IParser)Activator.CreateInstance(annotation.Parser);
                    return(parser);
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Unable to instantiate parser for - property:" + name + " of type:" + propertyClass.Name, ex);
            }

            Type type = propertyClass;

            if (this.STRING_TYPE == type)
            {
                parser = StandardParsers.STRING_PARSER;
            }
            else if (this.LONG_TYPE == type || this.LONG_PRIM == type)
            {
                parser = StandardParsers.LONG_PARSER;
            }
            else if (this.ULONG_TYPE == type || this.ULONG_PRIM == type)
            {
                parser = StandardParsers.ULONG_PARSER;
            }
            else if (this.INTEGER_TYPE == type || this.INTEGER_PRIM == type)
            {
                parser = StandardParsers.INT_PARSER;
            }
            else if (this.UINTEGER_TYPE == type || this.UINTEGER_PRIM == type)
            {
                parser = StandardParsers.UINT_PARSER;
            }
            else if (this.DOUBLE_TYPE == type || this.DOUBLE_PRIM == type)
            {
                parser = StandardParsers.DOUBLE_PARSER;
            }
            else if (this.DATE_TYPE == type)
            {
                parser = StandardParsers.DATETIME_PARSER;
            }
            else if (this.BOOLEAN_TYPE == type || this.BOOLEAN_PRIM == type)
            {
                parser = StandardParsers.BOOL_PARSER;
            }
            else if (this.SHORT_TYPE == type || this.SHORT_PRIM == type)
            {
                parser = StandardParsers.SHORT_PARSER;
            }
            else if (this.BYTE_TYPE == type || this.BYTE_PRIM == type)
            {
                parser = StandardParsers.BYTE_PARSER;
            }
            else if (this.SBYTE_TYPE == type || this.SBYTE_PRIM == type)
            {
                parser = StandardParsers.SBYTE_PARSER;
            }
            else if (this.FLOAT_TYPE == type || this.FLOAT_PRIM == type)
            {
                parser = StandardParsers.FLOAT_PARSER;
            }
            else if (this.CHAR_TYPE == type || this.CHAR_PRIM == type)
            {
                parser = StandardParsers.CHAR_PARSER;
            }
            else if (type.IsEnum)
            {
                parser = StandardParsers.ENUM_PARSER;
            }
            else if (this.GUID_TYPE == type || this.GUID_PRIM == type)
            {
                parser = StandardParsers.GUID_PARSER;
            }
            else if (type.GenericTypeArguments != null && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments[0].IsEnum)
            {
                parser = StandardParsers.ENUM_NULLABLE_PARSER;
            }
            else
            {
                throw new ApplicationException(
                          "Unable to find parser for property: " + name + ", of type: " + type.FullName +
                          ". Property class either must derive from Dtobase or You must setup custom parser for this property in the Dto attribute.");
            }

            return(parser);
        }