/// <summary> /// Gets an attribute of a given type and then casts and returns a specified value /// </summary> /// <typeparam name="X">The type of the attribute</typeparam> /// <typeparam name="Y">The type of the value to return</typeparam> /// <param name="o">The object to check</param> /// <param name="PropertyName">The property name to look for</param> /// <param name="Default">Default to return if property or attribute does not exist</param> /// <returns>Either the casted property, or default</returns> public static Y AttributeRef <X, Y>(this IHasAttributes o, string PropertyName, Y Default) where Y : class { if (!o.HasAttribute <X>() || !o.Attribute <X>().HasProperty(PropertyName)) { return(Default); } return(o.Attribute <X>().GetProperty(PropertyName).GetValue <Y>() ?? Default); }
/// <summary> /// Gets an attribute of a given type and returns a nullable? of its value /// </summary> /// <typeparam name="X">The type of the attribute</typeparam> /// <typeparam name="Y">The type of the value to return</typeparam> /// <param name="o">The object to check</param> /// <param name="PropertyName">The property name to look for</param> /// <returns>A nullable struct representation of its value</returns> public static Y?AttributeNullable <X, Y>(this IHasAttributes o, string PropertyName) where Y : struct { if (!o.HasAttribute <X>() || !o.Attribute <X>().HasProperty(PropertyName)) { return(null); } return(o.Attribute <X>().GetProperty(PropertyName).GetValue <Y>()); }
/// <summary> /// Retrieves a property value of an attribute of a given type (non-reference) /// </summary> /// <typeparam name="Y">The type of the property to return</typeparam> /// <param name="o">The object to check</param> /// <param name="t">The type of the attribute to search for</param> /// <param name="PropertyName">The name of the property to retrieve the value for</param> /// <param name="Default">If the property is not found, this is the default to return in place of null</param> /// <returns>Either the casted property, or default</returns> public static Y AttributeStruct <Y>(this IHasAttributes o, Type t, string PropertyName, Y Default) where Y : struct { if (!o.HasAttribute(t) || !o.Attribute(t).HasProperty(PropertyName)) { return(Default); } return(o.Attribute(t).GetProperty(PropertyName).GetValue <Y>()); }
/// <summary> /// Gets an attribute of a given type and then casts and returns a specified value /// </summary> /// <typeparam name="X">The type of the attribute to retrieve</typeparam> /// <typeparam name="Y">The type of the value to return</typeparam> /// <param name="o">The source of the attribute</param> /// <param name="PropertyName">The name of the property on the attribute to retrieve</param> /// <returns>The casted property value found on the attribute</returns> public static Y GetAttributeValue <X, Y>(this IHasAttributes o, string PropertyName) { return(o.Attribute <X>()[PropertyName].GetValue <Y>()); }