50 lines
1.9 KiB
C#
50 lines
1.9 KiB
C#
using System;
|
|
|
|
namespace XRLib.Util
|
|
{
|
|
public static class EnumExtension
|
|
{
|
|
public static void AddFlag<TEnum>(this ref TEnum currentValue, TEnum flagToAdd) where TEnum : struct, Enum
|
|
{
|
|
// currentValue와 flagToAdd가 같은 Enum 타입인지 확인합니다.
|
|
if (!typeof(TEnum).IsEnum)
|
|
{
|
|
throw new ArgumentException("TEnum must be an enumerated type.");
|
|
}
|
|
|
|
// currentValue와 flagToAdd를 숫자형식으로 변환한 후 OR 연산합니다.
|
|
int currentValueInt = Convert.ToInt32(currentValue);
|
|
int flagToAddInt = Convert.ToInt32(flagToAdd);
|
|
int resultInt = currentValueInt | flagToAddInt;
|
|
|
|
// OR 연산 결과를 Enum 타입으로 변환하여 currentValue 매개변수에 할당합니다.
|
|
currentValue = (TEnum)Enum.ToObject(typeof(TEnum), resultInt);
|
|
}
|
|
public static void RemoveFlag<TEnum>(this ref TEnum currentValue, TEnum flagToAdd) where TEnum : struct, Enum
|
|
{
|
|
// currentValue와 flagToAdd가 같은 Enum 타입인지 확인합니다.
|
|
if (!typeof(TEnum).IsEnum)
|
|
{
|
|
throw new ArgumentException("TEnum must be an enumerated type.");
|
|
}
|
|
|
|
// currentValue와 flagToAdd를 숫자형식으로 변환한 후 OR 연산합니다.
|
|
int currentValueInt = Convert.ToInt32(currentValue);
|
|
int flagToAddInt = Convert.ToInt32(flagToAdd);
|
|
int resultInt = currentValueInt & ~flagToAddInt;
|
|
|
|
// OR 연산 결과를 Enum 타입으로 변환하여 currentValue 매개변수에 할당합니다.
|
|
currentValue = (TEnum)Enum.ToObject(typeof(TEnum), resultInt);
|
|
}
|
|
public static bool StringToEnum<T>(string value, out T result)
|
|
{
|
|
result = default;
|
|
if (!Enum.TryParse(typeof(T), value, out var parse))
|
|
return false;
|
|
|
|
result = (T)parse;
|
|
return true;
|
|
}
|
|
|
|
}
|
|
} |