mirror of
https://github.com/tribufu/tribufu-dotnet
synced 2025-06-16 10:24:18 +00:00
Add dotnet-utils submodule
This commit is contained in:
@ -1,30 +0,0 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace Tribufu.ComponentModel
|
||||
{
|
||||
public class EnumMemberConverter<T> : EnumConverter
|
||||
{
|
||||
public EnumMemberConverter(Type type) : base(type) { }
|
||||
|
||||
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
|
||||
{
|
||||
var type = typeof(T);
|
||||
|
||||
foreach (var field in type.GetFields())
|
||||
{
|
||||
if (Attribute.GetCustomAttribute(field, typeof(EnumMemberAttribute)) is EnumMemberAttribute attribute && value is string enumValue && attribute.Value == enumValue)
|
||||
{
|
||||
return field.GetValue(null);
|
||||
}
|
||||
}
|
||||
|
||||
return base.ConvertFrom(context, culture, value);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user