58 lines
1.9 KiB
C#
58 lines
1.9 KiB
C#
using System;
|
|
using Kreta.Core.Validation.Exceptions;
|
|
using Kreta.Core.Validation.Exceptions.Enum;
|
|
using Kreta.Resources;
|
|
|
|
namespace Kreta.BusinessLogic.Classes.MobileApi.Naplo.V2.Co.Enum
|
|
{
|
|
public class NaploEnumCo<T> where T : struct, IConvertible
|
|
{
|
|
public static implicit operator NaploEnumCo<T>(int id) => new NaploEnumCo<T>(id);
|
|
public static implicit operator NaploEnumCo<T>(T enumValue) => new NaploEnumCo<T>(Convert.ToInt32(enumValue));
|
|
public static bool operator ==(NaploEnumCo<T> lhsWrapperCo, T rhsEnum) => lhsWrapperCo.GetEnum().Equals(rhsEnum);
|
|
public static bool operator !=(NaploEnumCo<T> lhsWrapperCo, T rhsEnum) => !lhsWrapperCo.GetEnum().Equals(rhsEnum);
|
|
|
|
public string EnumTypeNameWPostFix => typeof(T).Name;
|
|
public string EnumTypeName => EnumTypeNameWPostFix.Replace("Enum", "").Replace("enum", "");
|
|
public bool IsGeneratedEnum => !string.IsNullOrWhiteSpace(Nev);
|
|
|
|
public T GetEnum()
|
|
{
|
|
T enumValue;
|
|
|
|
if (!System.Enum.TryParse<T>(Nev, out enumValue))
|
|
{
|
|
throw new ValidationException(ValidationErrorType.ResourceNotFound, ErrorResource.NemEngedelyezettVagyNemLetezoEnum);
|
|
}
|
|
|
|
return enumValue;
|
|
}
|
|
public int Id { get; private set; }
|
|
public string Nev { get; private set; }
|
|
|
|
public NaploEnumCo(int id)
|
|
{
|
|
Fill(id);
|
|
if (!typeof(T).IsEnum)
|
|
{
|
|
throw new ArgumentException("T must be an enum");
|
|
}
|
|
}
|
|
|
|
private void Fill(int id)
|
|
{
|
|
Id = id;
|
|
Nev = System.Enum.GetName(typeof(T), id);
|
|
if (!typeof(T).IsEnum)
|
|
{
|
|
throw new ArgumentException("T must be an enum");
|
|
}
|
|
}
|
|
|
|
public NaploEnumCo(int id, string nev)
|
|
{
|
|
Id = id;
|
|
Nev = nev;
|
|
}
|
|
}
|
|
}
|