Files
DP44/DataPRO/Users/UserSettings/PropertyIdAttribute.cs
2026-04-17 14:55:32 -04:00

54 lines
2.2 KiB
C#

using System;
using System.Linq;
// ReSharper disable once CheckNamespace
namespace DTS.Slice.Users.UserSettings
{
/// <summary>
/// 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
/// </summary>
public class PropertyIdAttribute : Attribute
{
private readonly PropertyEnums.PropertyIds _propertyId;
/// <summary>
/// returns the property id from a property
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
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;
}
/// <summary>
/// returns the propertyid (as an enum) from a property
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
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;
}
}
}