Exemplo n.º 1
0
        public static void ValidateEntity(object entity)
        {
            var meta = MetaDataHelper.GetMetaData(entity.GetType());

            foreach (var prop in meta.Properties.Values)
            {
                var val = prop.GetValue(entity);
                if (prop.RequiredAttribute != null)
                {
                    if (val == null)
                    {
                        throw new ValidationException(prop.DisplayName + "字段必填");
                    }
                    if (prop.PropertyInfo.PropertyType == typeof(string) && val.ToString().IsNullOrWhiteSpace())
                    {
                        var errMsg = prop.RequiredAttribute.ErrorMessage ?? prop.DisplayName + "字段必填";
                        throw new ValidationException(errMsg);
                    }
                }
                if (prop.StringLengthAttribute != null)
                {
                    if (val != null && val.ToString().Length > prop.StringLengthAttribute.MaximumLength)
                    {
                        var errMsg = prop.StringLengthAttribute.ErrorMessage ?? prop.DisplayName + "字段长度不能超过" + prop.StringLengthAttribute;
                        throw new ValidationException(errMsg);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public bool Send <T>(string to, string subject, string path, T t)
        {
            var temp = string.Empty;

            using (StreamReader reader = new StreamReader(path, Encoding.UTF8)) {
                temp = reader.ReadToEnd();
            }
            var reg        = new Regex(@"({{.*?}})");
            var siteMeta   = MetaDataHelper.GetMetaData <SiteInfo>();
            var entityMeta = MetaDataHelper.GetMetaData <T>();
            var content    = reg.Replace(temp, (m) =>
            {
                var prop = m.Value.Replace("{", "").Replace("}", "");
                if (siteMeta.Properties.ContainsKey(prop))
                {
                    return(siteMeta.Properties[prop].GetValue(SiteInfo).ToString());
                }
                else if (entityMeta.Properties.ContainsKey(prop))
                {
                    return(entityMeta.Properties[prop].GetValue(t).ToString());
                }
                return(string.Empty);
            });

            return(Send(to, subject, content));
        }
Exemplo n.º 3
0
        public static void Add <TEntity>(Type type)
        {
            var entityMetaData = MetaDataHelper.GetMetaData <TEntity>();
            var table          = TypeDescriptor.GetAttributes(type).OfType <TableAttribute>().FirstOrDefault();

            if (table == null)
            {
                TypeDescriptor.AddAttributes(type, new TableAttribute(entityMetaData.TableName));
            }
            TypeDescriptionProvider parent = TypeDescriptor.GetProvider(type);
            var entityType = typeof(TEntity);
            MyTypeDescriptionProvider td = new MyTypeDescriptionProvider(parent, entityType);


            TypeDescriptor.AddProvider(td, type);
        }
Exemplo n.º 4
0
        public MyCustomTypeDescriptor(ICustomTypeDescriptor parent, Type genType)
            : base(parent)
        {
            EntityType = genType;
            var propertyCollections = parent.GetProperties();
            var entityMetaData      = MetaDataHelper.GetMetaData(EntityType);

            var entityProps = entityMetaData.Properties;
            List <PropertyDescriptor> propertyList = new List <PropertyDescriptor>();

            foreach (PropertyDescriptor descriptor in propertyCollections)
            {
                var myProperty     = new MyPropertyDescriptor(descriptor);
                var entityProperty = entityProps[myProperty.Name];

                if (entityProperty != null)
                {
                    List <Attribute> attriList = new List <Attribute>();
                    for (int i = 0; i < entityProperty.Attributes.Count; i++)
                    {
                        attriList.Add(entityProperty.Attributes[i]);
                    }
                    if (attriList.Count > 0)
                    {
                        myProperty.AddAttributes(attriList.ToArray());
                    }
                }
                else
                {
                    myProperty.AddAttributes(new NotMappedAttribute());
                }

                propertyList.Add(myProperty);
            }
            _propertyCollections = new PropertyDescriptorCollection(propertyList.ToArray());
        }
Exemplo n.º 5
0
        public static void SetValue <T>(T entity, string propName, object newVal)
        {
            var meta = MetaDataHelper.GetMetaData(entity.GetType());

            meta.Properties[propName].SetValue(entity, newVal);
        }
Exemplo n.º 6
0
        public static object GetValue <T>(T entity, string propName)
        {
            var meta = MetaDataHelper.GetMetaData(entity.GetType());

            return(meta.Properties[propName].GetValue(entity));
        }