mirror of
https://github.com/Tribufu/TribufuDotNet
synced 2026-08-08 04:11:06 +00:00
* Migrate to new tribufu project structure * Update .gitignore * Remove old api files * Update Directory.Build.props * Rename solution
30 lines
930 B
C#
30 lines
930 B
C#
// 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);
|
|
}
|
|
}
|
|
}
|