namespace System.Web.DynamicData.Util {
using System;
using System.Collections.Generic;
using System.Linq;
internal static class AttributeExtensions {
///
/// Gets the first attribute of a given time on the target AttributeCollection, or null.
///
/// The attribute type
/// The AttributeCollection object
///
internal static TAttribute FirstOrDefault(this System.ComponentModel.AttributeCollection attributes) where TAttribute : Attribute {
return attributes.OfType().FirstOrDefault();
}
internal static TResult GetAttributePropertyValue(this System.ComponentModel.AttributeCollection attributes, Func propertyGetter)
where TResult : class
where TAttribute : Attribute {
return attributes.GetAttributePropertyValue(propertyGetter, null);
}
internal static TResult GetAttributePropertyValue(this System.ComponentModel.AttributeCollection attributes, Func propertyGetter, TResult defaultValue)
where TAttribute : Attribute {
var attribute = attributes.FirstOrDefault();
return attribute.GetPropertyValue(propertyGetter, defaultValue);
}
///
/// Gets the property for a given attribute reference or returns null if the reference is null.
///
/// The attribute type
/// The type of the attribute's property
/// The attribute reference
/// The function to evaluate on the attribute
///
internal static TResult GetPropertyValue(this TAttribute attribute, Func propertyGetter)
where TResult : class
where TAttribute : Attribute {
return attribute.GetPropertyValue(propertyGetter, null);
}
///
/// Gets the property for a given attribute reference or returns the default value if the reference is null.
///
/// The attribute type
/// The type of the attribute's property
/// The attribute reference
/// The function to evaluate on the attribute
/// The default value to return if the attribute is null
///
internal static TResult GetPropertyValue(this TAttribute attribute, Func propertyGetter, TResult defaultValue)
where TAttribute : Attribute {
if (attribute != null) {
return propertyGetter(attribute);
}
else {
return defaultValue;
}
}
}
}