using System; using System.Linq; // ReSharper disable once CheckNamespace namespace DTS.Slice.Users.UserSettings { /// /// this PropertyId attribute is a little more complex than some of the other attributes /// it can be associated with an enum or an integer, in the db obviously properties are keyed by /// property id as an integer, but for convenience in code we use the enum form /// this attribute lets you store the property id for a property and get either the id or enum value /// public class PropertyIdAttribute : Attribute { private readonly PropertyEnums.PropertyIds _propertyId; /// /// returns the property id from a property /// /// /// public static int GetPropertyId(System.Reflection.PropertyInfo o) { if (o == null) throw new NullReferenceException("GetPropertyId called on a null reference"); var attr = o.GetCustomAttributes(typeof(PropertyIdAttribute), false); if (null == attr || 0 == attr.Length) { throw new NullReferenceException("GetPropertyId called on an object with no PropertyIdAttribute, object: " + o); } return (int)((PropertyIdAttribute)attr.First())._propertyId; } /// /// returns the propertyid (as an enum) from a property /// /// /// public static PropertyEnums.PropertyIds GetPropertyIdEnum(System.Reflection.PropertyInfo o) { if (o == null) throw new NullReferenceException(); var attr = o.GetCustomAttributes(typeof(PropertyIdAttribute), false); if (null == attr || 0 == attr.Length) { throw new NullReferenceException(); } return ((PropertyIdAttribute)attr.First())._propertyId; } public PropertyIdAttribute(PropertyEnums.PropertyIds propertyId) { _propertyId = propertyId; } } }