Enum에 확장 메서드를 추가하는 방법
이 Enum 코드가 있습니다.
enum Duration { Day, Week, Month };
이 Enum에 대한 확장 메서드를 추가 할 수 있습니까?
이 사이트 에 따르면 :
확장 메서드는 팀의 다른 사람들이 실제로 발견하고 사용할 수있는 방식으로 기존 클래스에 대한 메서드를 작성하는 방법을 제공합니다. 열거 형이 다른 클래스와 같은 클래스라는 점을 감안할 때 다음과 같이 확장 할 수 있다는 것은 그리 놀라운 일이 아닙니다.
enum Duration { Day, Week, Month };
static class DurationExtensions
{
public static DateTime From(this Duration duration, DateTime dateTime)
{
switch (duration)
{
case Day: return dateTime.AddDays(1);
case Week: return dateTime.AddDays(7);
case Month: return dateTime.AddMonths(1);
default: throw new ArgumentOutOfRangeException("duration");
}
}
}
일반적으로 열거 형이 최선의 선택은 아니지만 적어도 이것은 스위치 / if 처리 중 일부를 중앙 집중화하고 더 나은 일을 할 수있을 때까지 추상화 할 수 있습니다. 값도 범위 내에 있는지 확인하십시오.
여기 Microsft MSDN에서 자세한 내용을 읽을 수 있습니다 .
Enum의 인스턴스가 아닌 Enum 유형에 확장 메서드를 추가 할 수도 있습니다.
/// <summary> Enum Extension Methods </summary>
/// <typeparam name="T"> type of Enum </typeparam>
public class Enum<T> where T : struct, IConvertible
{
public static int Count
{
get
{
if (!typeof(T).IsEnum)
throw new ArgumentException("T must be an enumerated type");
return Enum.GetNames(typeof(T)).Length;
}
}
}
다음을 수행하여 위의 확장 메서드를 호출 할 수 있습니다.
var result = Enum<Duration>.Count;
진정한 확장 방법이 아닙니다. Enum <>이 System.Enum과 다른 유형이기 때문에 작동합니다.
물론 예를 들어 다음 DescriptionAttribue
과 같이 enum
값 을 사용하고 싶을 수 있습니다 .
using System.ComponentModel.DataAnnotations;
public enum Duration
{
[Description("Eight hours")]
Day,
[Description("Five days")]
Week,
[Description("Twenty-one days")]
Month
}
이제 다음과 같이 할 수 있기를 원합니다.
Duration duration = Duration.Week;
var description = duration.GetDescription(); // will return "Five days"
확장 방법 GetDescription()
은 다음과 같이 작성할 수 있습니다.
using System.ComponentModel;
using System.Reflection;
public static string GetDescription(this Enum value)
{
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
if (fieldInfo == null) return null;
var attribute = (DescriptionAttribute)fieldInfo.GetCustomAttribute(typeof(DescriptionAttribute));
return attribute.Description;
}
모든 답변은 훌륭하지만 특정 유형의 열거 형에 확장 메서드를 추가하는 것에 대해 이야기하고 있습니다.
명시 적 캐스팅 대신 현재 값의 int를 반환하는 것과 같은 모든 열거 형에 메서드를 추가하려면 어떻게해야합니까?
public static class EnumExtensions
{
public static int ToInt<T>(this T soure) where T : IConvertible//enum
{
if (!typeof(T).IsEnum)
throw new ArgumentException("T must be an enumerated type");
return (int) (IConvertible) soure;
}
//ShawnFeatherly funtion (above answer) but as extention method
public static int Count<T>(this T soure) where T : IConvertible//enum
{
if (!typeof(T).IsEnum)
throw new ArgumentException("T must be an enumerated type");
return Enum.GetNames(typeof(T)).Length;
}
}
뒤의 트릭 IConvertible
은 상속 계층 구조입니다. MDSN 참조
그의 답변에 대해 ShawnFeatherly에게 감사드립니다.
You can create an extension for anything, even object
(although that's not considered best-practice). Understand an extension method just as a public static
method. You can use whatever parameter-type you like on methods.
public static class DurationExtensions
{
public static int CalculateDistanceBetween(this Duration first, Duration last)
{
//Do something here
}
}
See MSDN.
public static class Extensions
{
public static string SomeMethod(this Duration enumValue)
{
//Do something here
return enumValue.ToString("D");
}
}
we have just made an enum extension for c# https://github.com/simonmau/enum_ext
It's just a implementation for the typesafeenum, but it works great so we made a package to share - have fun with it
public sealed class Weekday : TypeSafeNameEnum<Weekday, int>
{
public static readonly Weekday Monday = new Weekday(1, "--Monday--");
public static readonly Weekday Tuesday = new Weekday(2, "--Tuesday--");
public static readonly Weekday Wednesday = new Weekday(3, "--Wednesday--");
....
private Weekday(int id, string name) : base(id, name)
{
}
public string AppendName(string input)
{
return $"{Name} {input}";
}
}
I know the example is kind of useless, but you get the idea ;)
참고URL : https://stackoverflow.com/questions/15388072/how-to-add-extension-methods-to-enums
'development' 카테고리의 다른 글
요소가 애니메이션되는 경우 jQuery로 어떻게 알 수 있습니까? (0) | 2020.08.21 |
---|---|
JTextField에서 Enter Press 감지 (0) | 2020.08.21 |
Comparator.reversed ()는 람다를 사용하여 컴파일되지 않습니다. (0) | 2020.08.21 |
iOS5 스토리 보드 오류 : iOS 4.3 및 이전 버전에서는 스토리 보드를 사용할 수 없습니다. (0) | 2020.08.21 |
iOS의 NSUserDefaults에 해당하는 Android (0) | 2020.08.21 |