C# DescriptionAttribute 取值


 public static class Extensions
    {
      
        public static string GetDescription(this object obj, string name)
        {  
                var referencedType = obj.GetType(); 
                return GetDescription(referencedType, name);
        }

        public static string GetDescription(Type referencedType, string propertyName)
        {
            //	gets the properties of the referenced type
            PropertyDescriptorCollection properties
                = TypeDescriptor.GetProperties(referencedType);

            if (properties != null)
            {

                // gets a PropertyDescriptor to the specific property.
                PropertyDescriptor property = properties[propertyName];
                if (property != null)
                {
                    //  gets the attributes of the required property
                    AttributeCollection attributes = property.Attributes;

                    // Gets the description attribute from the collection.
                    DescriptionAttribute descript
                        = (ObjectDescriptionAttribute)attributes[typeof(ObjectDescriptionAttribute)];

                    // register the referenced description
                    if (!String.IsNullOrEmpty(descript.Description))
                    { return descript.Description; }
                }

            }

            return string.Empty;
        }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM