Attribute

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace LotteryTicket.Entity
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class | AttributeTargets.Field, AllowMultiple = true, Inherited = false)]
    public class XmlNodeNameAttribute : Attribute
    {
        private string _Name;
        public XmlNodeNameAttribute(string XmlNodeName)
        {
            this._Name = XmlNodeName;
        }

        public static string GetNodeName(Type T, string Name)
        {
            if (T.Name == Name)
            {
                object[] Ats = T.GetCustomAttributes(false);
                foreach (object A in Ats)
                {
                    if (A.GetType() == typeof(XmlNodeNameAttribute))
                    {
                        return ((XmlNodeNameAttribute)A)._Name;
                    }
                }
            }

            MemberInfo[] Mis = T.GetMember(Name);
            foreach (MemberInfo Mi in Mis)
            {
                if (Mi.IsDefined(typeof(XmlNodeNameAttribute), false))
                {
                    object[] Attrs = Mi.GetCustomAttributes(false);
                    foreach (object Attr in Attrs)
                    {
                        if (Attr.GetType() == typeof(XmlNodeNameAttribute))
                        {
                            XmlNodeNameAttribute Att = (XmlNodeNameAttribute)Attr;
                            return Att._Name;
                        }
                    }
                }
            }
            return "未标记XmlNodeNameAttribute";
        }

        #region 废弃 - 已注释
        //public static XElement CreateCommonXElement<T>(T Entity)
        //{
        //    XElement X = null;
        //    object[] Ats = Entity.GetType().GetCustomAttributes(false);
        //    foreach (object A in Ats)
        //    {
        //        if (A.GetType() == typeof(XmlNodeNameAttribute))
        //        {
        //            X = new XElement(XmlNodeNameAttribute.GetNodeName(Entity.GetType(), Entity.GetType().Name));
        //        }
        //    }

        //    PropertyInfo[] Props = Entity.GetType().GetProperties();
        //    foreach (PropertyInfo Prop in Props)
        //    {
        //        if (Prop.IsDefined(typeof(XmlNodeNameAttribute), false))
        //        {
        //            string XmlNodeName = string.Empty;
        //            object[] Attrs = Prop.GetCustomAttributes(false);
        //            foreach (object Attr in Attrs)
        //            {
        //                if (Attr.GetType() == typeof(XmlNodeNameAttribute))
        //                {
        //                    XmlNodeNameAttribute Att = (XmlNodeNameAttribute)Attr;
        //                    XmlNodeName = XmlNodeNameAttribute.GetNodeName(typeof(T), Prop.Name);//读取XmlNodeAttribute节点名称
        //                }
        //            }

        //            X.Add(new XElement(XmlNodeName, Prop.GetValue(Entity, null)));
        //        }
        //    }
        //    return X;
        //}
        #endregion
    }
}

 

posted @ 2013-09-17 17:17  刘文天  阅读(336)  评论(0编辑  收藏  举报