Ahjesusカスタム属性Attributeまたは属性の名前を取得

15653 ワード

1:独自のカスタム属性を設定する
    public class NameAttribute:Attribute {

        private string _description;

        public NameAttribute(string description) {

            _description = description;

        }

        

        public string Description {

            get { return _description; }

        }

    }

2:属性または属性名を取得するクラスを設定する
    /// <summary>

    ///  Name 

    /// </summary>

    /// <typeparam name="T"> </typeparam>

    public class AttributeHelper<T> where T : new() {        

        /// <summary>

        ///  Name

        /// </summary>

        /// <param name="type"> </param>

        /// <returns></returns>

        public string NameFor(object type) {

            T test = (T)type;

            FieldInfo fieldInfo = test.GetType().GetField(test.ToString());

            object[] attribArray = fieldInfo.GetCustomAttributes(false);

            //IList<CustomAttributeData> list = fieldInfo.GetCustomAttributesData();

            string des = (attribArray[0] as NameAttribute).Description;

            return des;

        }



        /// <summary>

        ///  Name

        /// </summary>

        /// <param name="predicate"> </param>

        /// <returns></returns>

        public string NameFor(Expression<Func<T, object>> expr) {

            string name = PropertyNameFor(expr);

            T et = new T();

            Type type = et.GetType();

            PropertyInfo[] properties = type.GetProperties();

            object[] attributes = null;

            foreach (PropertyInfo p in properties) {

                if (p.Name == name) {

                    attributes = p.GetCustomAttributes(typeof(NameAttribute), true);

                    break;

                }

            }// http://www.cnblogs.com/ahjesus  , , !
string des = ((NameAttribute)attributes[0]).Description; return des; } /// <summary> /// /// </summary> /// <param name="expr"></param> /// <returns></returns> public string PropertyNameFor(Expression<Func<T, object>> expr) { var rtn = ""; if (expr.Body is UnaryExpression) { rtn = ((MemberExpression)((UnaryExpression)expr.Body).Operand).Member.Name; }// http://www.cnblogs.com/ahjesus , , !
       else if (expr.Body is MemberExpression) { rtn = ((MemberExpression)expr.Body).Member.Name; } else if (expr.Body is ParameterExpression) { rtn = ((ParameterExpression)expr.Body).Type.Name; } return rtn; } }

3:テストエンティティクラスと列挙の設定
    public class MyEntity {

        public MyEntity() {

            Name = "Jude";

            Age = 11;

        }

        [Name(" ")]

        public string Name { get; set; }

        [Name(" ")]

        public int Age { get; set; }

    }
    public enum MyEnum {

        [Name(" ")]

        Europe = 0,

        [Name(" ")]

        Asia = 1,

        [Name(" ")]

        America = 2

    }

4:テスト開始
    public partial class WebForm1 : System.Web.UI.Page {

        protected void Page_Load(object sender, EventArgs e) {

            AttributeHelper<MyEntity> myEntityAttr = new AttributeHelper<MyEntity>();

            MyEntity myEntity = new MyEntity();

            AttributeHelper<MyEnum> myEnumAttr = new AttributeHelper<MyEnum>();

            Response.Write(myEntityAttr.NameFor(it => it.Name) + ":" + myEntity.Name + "
");// :Jude Response.Write(myEntityAttr.NameFor(it => it.Age) + ":" + myEntity.Age + "
");// :11 Response.Write(myEntityAttr.PropertyNameFor(it => it.Name) + ":" + myEntity.Name + "
");//Name:Jude Response.Write(myEntityAttr.PropertyNameFor(it => it.Age) + ":" + myEntity.Age + "
");//Age:11 // http://www.cnblogs.com/ahjesus  , , ! Response.Write(myEnumAttr.NameFor(MyEnum.America) + ":" + MyEnum.America + "
");// :America Response.Write(myEnumAttr.NameFor(MyEnum.Asia) + ":" + MyEnum.Asia + "
");// :Asia Response.Write(myEnumAttr.NameFor(MyEnum.Europe) + ":" + MyEnum.Europe + "
");// :Europe } }