Files
2025-12-11 11:39:02 +08:00

60 lines
1.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* Copyright (C) Alibaba Cloud Computing
* All rights reserved.
*
* 版权所有 C阿里云计算有限公司
*/
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Aliyun.Api.LOG.Common.Utilities
{
/// <summary>
/// Description of EnumUtils.
/// </summary>
internal static class EnumUtils
{
private static IDictionary<Enum, StringValueAttribute> _stringValues =
new Dictionary<Enum, StringValueAttribute>();
public static string GetStringValue(this Enum value)
{
string output = null;
Type type = value.GetType();
if (_stringValues.ContainsKey(value))
{
output = (_stringValues[value] as StringValueAttribute).Value;
}
else
{
FieldInfo fi = type.GetField(value.ToString());
StringValueAttribute[] attrs =
fi.GetCustomAttributes(typeof (StringValueAttribute),
false) as StringValueAttribute[];
if (attrs.Length > 0)
{
output = attrs[0].Value;
// Put it in the cache.
lock(_stringValues)
{
// Double check
if (!_stringValues.ContainsKey(value))
{
_stringValues.Add(value, attrs[0]);
}
}
}
else
{
return value.ToString();
}
}
return output;
}
}
}