//Microsoft Public License (Ms-PL)
//http://visualizer.codeplex.com/license
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
namespace Redwerb.BizArk.Core.AttributeExt
{
///
/// Provides extension methods for PropertyDescriptor.
///
public static class AttributeExt
{
///
/// Gets the specified attribute from the PropertyDescriptor.
///
///
///
///
public static T GetAttribute(this PropertyDescriptor prop) where T : Attribute
{
foreach (Attribute att in prop.Attributes)
{
var tAtt = att as T;
if (tAtt != null) return tAtt;
}
return null;
}
///
/// Gets the specified attribute from the type.
///
///
///
///
///
public static T GetAttribute(this Type type, bool inherit) where T : Attribute
{
var atts = type.GetCustomAttributes(typeof(T), inherit);
if (atts.Length == 0) return null;
return atts[0] as T;
}
///
/// Gets the specified attribute for the assembly.
///
///
///
///
public static T GetAttribute(this Assembly asm) where T : Attribute
{
if (asm == null) return null;
var atts = asm.GetCustomAttributes(typeof(T), false);
if (atts == null) return null;
if (atts.Length == 0) return null;
return (T)atts[0];
}
///
/// Gets the specified attribute from the PropertyDescriptor.
///
///
///
///
///
public static T GetAttribute(this object obj, bool inherit) where T : Attribute
{
if (obj == null) return null;
return obj.GetType().GetAttribute(inherit);
}
///
/// Gets the specified attribute for the assembly.
///
///
///
///
public static T GetAttribute(this Enum val) where T : Attribute
{
var fi = val.GetType().GetField(val.ToString());
var atts = fi.GetCustomAttributes(typeof(T), false);
if (atts.Length == 0) return null;
return (T)atts[0];
}
///
/// Gets the description for the enumerated value.
///
///
///
public static string GetDescription(this Enum e)
{
var att = GetAttribute(e);
if (att == null) return "";
return att.Description;
}
}
}