Ejemplo n.º 1
0
        /// <summary>
        /// 获取T4生成特性
        /// </summary>
        /// <param name="pro"></param>
        /// <returns></returns>
        public static T4PropertyInfo GetT4PropertyInfo(this PropertyInfo pro)
        {
            var t4ProInfo = new T4PropertyInfo()
            {
                Name     = pro.Name,
                Ignore   = pro.GetAttribute <T4GenerationIgnoreAttribute>(false) != null,
                DataType = pro.GetT4DataType()
            };

            //显示名
            var displayAttribute = pro.GetAttribute <DisplayAttribute>(false);

            t4ProInfo.DisplayName = displayAttribute == null ? null : displayAttribute.Name;
            //是否必填
            var requiredAttribute = pro.GetAttribute <RequiredAttribute>(false);

            t4ProInfo.Required = requiredAttribute != null;

            //字符串长度
            var StringLengthAttribute = pro.GetAttribute <StringLengthAttribute>(false);

            t4ProInfo.MaxLength = StringLengthAttribute == null ? (int?)null : StringLengthAttribute.MaximumLength;

            //描述
            var descriptionAttribute = pro.GetAttribute <DescriptionAttribute>(false);

            t4ProInfo.Description = descriptionAttribute == null ? null : descriptionAttribute.Description;

            //只读
            var t4ReadOnlyFieldAttribute = pro.GetAttribute <T4ReadOnlyFieldAttribute>(false);

            t4ProInfo._ReadOnly = t4ReadOnlyFieldAttribute != null;
            if (t4ProInfo._ReadOnly)
            {
                t4ProInfo._ReadOnlyType = t4ReadOnlyFieldAttribute.ReadOnlyType;
            }
            //组特性
            var t4FormGroupAttribute = pro.GetAttribute <T4FormGroupAttribute>(false);

            if (t4FormGroupAttribute != null)
            {
                t4ProInfo.T4GroupInfo = new T4.Models.T4GroupInfo()
                {
                    Name = t4FormGroupAttribute.GroupName
                };
            }

            var t4SelectAttribute = pro.GetAttribute <T4SelectAttribute>(false);

            if (t4SelectAttribute != null)
            {
                t4ProInfo.T4Select = t4SelectAttribute;
            }
            return(t4ProInfo);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取数据类型
        /// </summary>
        /// <param name="pro"></param>
        /// <returns></returns>
        public string T4FieldHtml(T4PropertyInfo t4ProInfo)
        {
            t4ProInfo.Tag = this.Tag;
            var dic = this.FieldTypeTemplates;
            var defaultHtmlTemplate = this.DefaultHtmlTemplate;

            //判断是否只读
            t4ProInfo.ReadOnly =
                (t4ProInfo._ReadOnly && t4ProInfo._ReadOnlyType == ReadOnlyTypes.All)
                ||
                (t4ProInfo.Tag.Equals("add", StringComparison.CurrentCultureIgnoreCase) && t4ProInfo._ReadOnly && (t4ProInfo._ReadOnlyType == ReadOnlyTypes.Add))
                ||
                (t4ProInfo.Tag.Equals("edit", StringComparison.CurrentCultureIgnoreCase) && t4ProInfo._ReadOnly && (t4ProInfo._ReadOnlyType == ReadOnlyTypes.Edit));

            if (t4ProInfo.Ignore || t4ProInfo.Name.Equals("id", StringComparison.CurrentCultureIgnoreCase))
            {
                return(null);
            }
            var temp = dic.ContainsKey(t4ProInfo.DataType) ? dic[t4ProInfo.DataType] : defaultHtmlTemplate;

            if (!string.IsNullOrEmpty(temp))
            {
                var str = temp;
                foreach (var proInfo in t4ProInfo.GetType().GetProperties())
                {
                    var name  = "{" + proInfo.Name + "}";
                    var value = (proInfo.GetValue(t4ProInfo) ?? string.Empty).ToString();
                    //bool类型小写
                    if (proInfo.PropertyType == typeof(bool))
                    {
                        value = value.ToLower();
                    }
                    str = str.Replace(name, value);
                }
                return(str);
            }
            return(string.Empty);
        }