【散分贴】mono在手,天下我有

newtee 2013-09-21 12:44:55

弄了份mono源码在看,最近在研究Attribute

有来调侃Attribute的吗?
...全文
171 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
newtee 2013-09-22
  • 打赏
  • 举报
回复
attribute要和反射一起用 不到万不得已不用
newtee 2013-09-22
  • 打赏
  • 举报
回复
引用 7 楼 a346729576 的回复:
我想用attribute来捕捉异常,一直都木有去研究。。。
log4net不就行了?
newtee 2013-09-21
  • 打赏
  • 举报
回复
夜色镇歌 2013-09-21
  • 打赏
  • 举报
回复
引用 5 楼 zhuankeshumo 的回复:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace ConsoleApplication
{
    public class Model1
    {
        [Required]
        public string Name { get; set; }

        [Cust]
        public int Age { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Model1 model = new Model1() {Name="212",Age=12345 };
            foreach (var item in model.IsValid())
            {
                Console.WriteLine("FieldName:{0} Error Message:{1}", item.FieldName, item.Message);
            }
            Console.ReadLine();
        }
    }

    public class CustAttribute : ValidationAttribute 
    {
        public CustAttribute()
            : base("应该为6位数")
        {

        }

        public override bool IsValid(object value)
        {
           Regex  reg=new Regex(@"[\d]{6}");
           if (reg.IsMatch(Convert.ToString(value)))
           {
               return true;
           }
           else
           {
               return false;
           }
        }
    }

    public class ModelValidationError
    {
        public string FieldName { get; set; }
        public string Message { get; set; }
    }

    public static class DataAnnotationHelper
    {
        public static IEnumerable<ModelValidationError> IsValid<T>(this T o)
        {
            var descriptor = GetTypeDescriptor(typeof(T));

            foreach (PropertyDescriptor propertyDescriptor in descriptor.GetProperties())
            {
                foreach (var validationAttribute in propertyDescriptor.Attributes.OfType<ValidationAttribute>())
                {
                    if (!validationAttribute.IsValid(propertyDescriptor.GetValue(o)))
                    {
                        yield return new ModelValidationError() { FieldName = propertyDescriptor.Name, Message = validationAttribute.FormatErrorMessage(propertyDescriptor.Name) };
                    }
                }
            }
        }
        private static ICustomTypeDescriptor GetTypeDescriptor(Type type)
        {
            return new AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type);
        }
    }
}
引用 2 楼 a346729576 的回复:
是不是很好用?
我想用attribute来捕捉异常,一直都木有去研究。。。
__岑__ 2013-09-21
  • 打赏
  • 举报
回复
newtee 2013-09-21
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace ConsoleApplication
{
    public class Model1
    {
        [Required]
        public string Name { get; set; }

        [Cust]
        public int Age { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Model1 model = new Model1() {Name="212",Age=12345 };
            foreach (var item in model.IsValid())
            {
                Console.WriteLine("FieldName:{0} Error Message:{1}", item.FieldName, item.Message);
            }
            Console.ReadLine();
        }
    }

    public class CustAttribute : ValidationAttribute 
    {
        public CustAttribute()
            : base("应该为6位数")
        {

        }

        public override bool IsValid(object value)
        {
           Regex  reg=new Regex(@"[\d]{6}");
           if (reg.IsMatch(Convert.ToString(value)))
           {
               return true;
           }
           else
           {
               return false;
           }
        }
    }

    public class ModelValidationError
    {
        public string FieldName { get; set; }
        public string Message { get; set; }
    }

    public static class DataAnnotationHelper
    {
        public static IEnumerable<ModelValidationError> IsValid<T>(this T o)
        {
            var descriptor = GetTypeDescriptor(typeof(T));

            foreach (PropertyDescriptor propertyDescriptor in descriptor.GetProperties())
            {
                foreach (var validationAttribute in propertyDescriptor.Attributes.OfType<ValidationAttribute>())
                {
                    if (!validationAttribute.IsValid(propertyDescriptor.GetValue(o)))
                    {
                        yield return new ModelValidationError() { FieldName = propertyDescriptor.Name, Message = validationAttribute.FormatErrorMessage(propertyDescriptor.Name) };
                    }
                }
            }
        }
        private static ICustomTypeDescriptor GetTypeDescriptor(Type type)
        {
            return new AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type);
        }
    }
}
引用 2 楼 a346729576 的回复:
是不是很好用?
newtee 2013-09-21
  • 打赏
  • 举报
回复
看错了 以为说楼主呢
newtee 2013-09-21
  • 打赏
  • 举报
回复
引用 2 楼 a346729576 的回复:
哪个attribute ? 类 属性 上面那个标记? =====木叶之夏木有JJ,另外反弹一切诅咒
夜色镇歌 2013-09-21
  • 打赏
  • 举报
回复
哪个attribute ? 类 属性 上面那个标记? =====楼下木有JJ,另外反弹一切诅咒

7,765

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧