47 lines
1.4 KiB
C#
47 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace OFBButte.Application.Configuration
|
|
{
|
|
public class Permission
|
|
{
|
|
public string Value { get; private set; }
|
|
private Permission(string permission)
|
|
{
|
|
Value = permission;
|
|
}
|
|
|
|
public static Permission AddProfile { get; } = new Permission("Add Profile");
|
|
|
|
private static Dictionary<string, Permission> values = typeof(Permission).GetProperties(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).Where(p => p.PropertyType == typeof(Permission)).ToDictionary(p => p.Name, p => (Permission)p.GetValue(null));
|
|
public static bool TryCast(string input, out Permission permission)
|
|
{
|
|
permission = null;
|
|
if (values.TryGetValue(input, out Permission perm))
|
|
{
|
|
permission = perm;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static implicit operator string(Permission input)
|
|
{
|
|
return input.Value;
|
|
}
|
|
|
|
public static explicit operator Permission(string input)
|
|
{
|
|
if (TryCast(input, out Permission permission))
|
|
{
|
|
return permission;
|
|
} else
|
|
{
|
|
throw new InvalidCastException($"Could not cast {input} to {typeof(Permission).ToString()}");
|
|
}
|
|
}
|
|
}
|
|
}
|