Ahjesus 사용자 정의 속성 Attribute 또는 속성 이름 가져오기

15759 단어 attribute
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 } }

 
 
 

좋은 웹페이지 즐겨찾기