本文整理汇总了C#中System.Enum.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# Enum.GetType方法的具体用法?C# Enum.GetType怎么用?C# Enum.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Enum
的用法示例。
在下文中一共展示了Enum.GetType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BuildListFromEnum
public static ListItemCollection BuildListFromEnum(Enum objEnum)
{
ListItemCollection colListItems = new ListItemCollection();
ListItem liItem;
SortedList colSortedListItems = new SortedList();
foreach (int value in Enum.GetValues(objEnum.GetType()))
{
liItem = new ListItem();
liItem.Value = value.ToString();
liItem.Text = StringEnum.GetStringValue((Enum)Enum.Parse(objEnum.GetType(), value.ToString(), true));
if (liItem.Text != string.Empty)
{
colSortedListItems.Add(liItem.Text, liItem);
}
liItem = null;
}
foreach (ListItem liListItem in colSortedListItems.GetValueList())
{
colListItems.Add(liListItem);
}
return colListItems;
}
示例2: GetAttribute
private static Attribute GetAttribute(Type t, Enum e)
{
var name = Enum.GetName(e.GetType(), e);
var fieldInfo = e.GetType().GetField(name);
return Attribute.GetCustomAttribute(fieldInfo, t);
}
示例3: GetMessageFormat
/// <summary>
/// Retrieves the message format from the DefaultMessage attribute decorating the
/// message code
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
protected string GetMessageFormat(Enum code)
{
System.Reflection.FieldInfo fi = code.GetType().GetField(code.ToString());
var attributes = (DefaultMessageAttribute[])fi.GetCustomAttributes(typeof(DefaultMessageAttribute), false);
return (attributes.Length > 0)
? attributes[0].Message
: code.GetType().DeclaringType.FullName + "." + code.ToString();
}
示例4: AnyFlag
public static bool AnyFlag(this Enum baseEnum, Enum value)
{
if (!baseEnum.GetType().IsEquivalentTo(value.GetType()))
{
throw ExceptionHelper.EnumTypeDoesNotMatch(value.GetType(), baseEnum.GetType());
}
return ((ToUInt64(baseEnum) & ToUInt64(value)) != 0);
}
示例5: EvaluateEnum
private void EvaluateEnum( Enum e )
{
Console.WriteLine( "Name: " + e.GetType().Name );
Console.WriteLine( "Underlying Type: " + Enum.GetUnderlyingType( e.GetType() ) );
Array values = Enum.GetValues(e.GetType());
for (int i = 0; i < values.Length; i++)
Console.WriteLine("Name: {0}, Value:{0:D}", values.GetValue(i));
}
示例6: EnumDialog
public EnumDialog(Enum input)
{
this.InitializeComponent();
this._enumType = input.GetType();
foreach (var name in Enum.GetNames(input.GetType()))
{
this.comboBox1.Items.Add(name);
}
this.comboBox1.Text = input.ToString();
}
示例7: CheckEnumHasFlagAttribute
static void CheckEnumHasFlagAttribute(Enum actual)
{
#if NewReflection
if (!actual.GetType().GetTypeInfo().IsDefined(typeof(FlagsAttribute), false))
#else
if (!actual.GetType().IsDefined(typeof(FlagsAttribute), false))
#endif
{
throw new ArgumentException("Enum doesn't have Flags attribute", nameof(actual));
}
}
示例8: EvaluateEnum
private static void EvaluateEnum(Enum e)
{
Console.WriteLine(String.Format("Information about {0}", e.GetType().Name));
Console.WriteLine(String.Format("Underlying type: {0}", e.GetTypeCode()));
var enumData = Enum.GetValues(e.GetType());
foreach (var item in enumData)
{
Console.WriteLine(String.Format("Name: {0} Value: {0:d} ", item));
}
Console.WriteLine();
}
示例9: EvaluateEnum
static void EvaluateEnum(Enum e)
{
Console.WriteLine("=> Information about {0}", e.GetType().Name);
Console.WriteLine("Underlying storage type: {0}", Enum.GetUnderlyingType(e.GetType()));
Array enumData = Enum.GetValues(e.GetType());
//var x = enumData[0];
Console.WriteLine("This enum has {0} members", enumData.Length);
for(int i = 0; i < enumData.Length; i++)
{
Console.WriteLine("Name: {0}, Value {0:D}", enumData.GetValue(i));
}
Console.WriteLine();
}
示例10: BytesFromPspIso
private static IList<byte> BytesFromPspIso( Stream iso, Enum file, int offset, int size )
{
if ( file.GetType() == typeof( FFTPack.Files ) )
{
return FFTPack.GetFileFromIso( iso, pspIsoInfo, (FFTPack.Files)file ).Sub( offset, offset + size - 1 );
}
else if ( file.GetType() == typeof( PatcherLib.Iso.PspIso.Sectors ) )
{
return PatcherLib.Iso.PspIso.GetFile( iso, pspIsoInfo, (PatcherLib.Iso.PspIso.Sectors)file, offset, size );
}
else
{
throw new ArgumentException();
}
}
示例11: GetStringValue
public static string GetStringValue(Enum value)
{
string output = null;
Type type = value.GetType();
//Check first in our cached results...
if (_stringValues.ContainsKey(value))
output = (_stringValues[value] as StringValueAttribute).Value;
else
{
//Look for our 'StringValueAttribute'
//in the field's custom attributes
FieldInfo fi = type.GetField(value.ToString());
StringValueAttribute[] attrs =
fi.GetCustomAttributes(typeof(StringValueAttribute),
false) as StringValueAttribute[];
if (attrs.Length > 0)
{
_stringValues.Add(value, attrs[0]);
output = attrs[0].Value;
}
}
return output;
}
示例12: EnumToString
public static string EnumToString(Enum enumValue)
{
StringBuilder enumValueAsString = new StringBuilder();
Type enumType = enumValue.GetType();
string[] enumToStringParts = enumValue.ToString().Split(ENUM_FLAGGED_VALUE_SEPERATOR_CHARACTER);
foreach (string enumValueStringPart in enumToStringParts)
{
FieldInfo enumValueField = enumType.GetField(enumValueStringPart.Trim());
EnumValueDescriptionAttribute[] enumDesc = enumValueField.GetCustomAttributes(typeof(EnumValueDescriptionAttribute), false) as
EnumValueDescriptionAttribute[];
if (enumValueAsString.Length > 0)
{
enumValueAsString.Append(ENUM_FLAGGED_VALUE_SEPERATOR_CHARACTER);
}
if (enumDesc.Length == 1)
{
enumValueAsString.Append(enumDesc[0].Description);
}
else
{
enumValueAsString.Append(enumValueStringPart);
}
}
return enumValueAsString.ToString();
}
示例13: GetEnumDescription
private static string GetEnumDescription(Enum value)
{
if (value == null) return null;
var type = value.GetType().GetTypeInfo();
var res = value.ToString();
if (type.CustomAttributes.Any(t => t.AttributeType == typeof(FlagsAttribute)))
{
var members = type.AsType().GetMembers();
var conversion = members.SelectMany(t => t.GetCustomAttributes(typeof(DescriptionAttribute), false).Select(a => new KeyValuePair<string, string>(t.Name, ((DescriptionAttribute)a).Description))).ToDictionary(t => t.Key, t => t.Value);
res = string.Join(",", res.Split(',').Select(t => t.Trim()).Select(t => conversion.ContainsKey(t) ? conversion[t] : t));
}
else
{
var memInfo = type.AsType().GetMember(value.ToString());
if (memInfo.Length > 0)
{
var attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false).ToList();
if (attrs.Any())
res = ((DescriptionAttribute)attrs.First()).Description;
}
}
return res;
}
示例14: GetStringValue
public static string GetStringValue(Enum value)
{
// Get the enum Type.
var type = value.GetType();
// Check if the enum already exits in the Dictionary.
if (StringValues.ContainsKey(value))
return StringValues[value].Value;
// Get the enum field info.
FieldInfo fi = type.GetField(value.ToString());
// Convert the enums to the strings.
EnumToStringAttribute[] attrs = fi.GetCustomAttributes(typeof(EnumToStringAttribute), false) as EnumToStringAttribute[];
// if the attributes are null or if the length of the array is equal to 0 or 0, return null.
if (attrs != null && attrs.Length <= 0)
return null;
if (attrs == null)
return null;
// add the enum to the Dictionary.
StringValues.Add(value, attrs[0]);
// Return the string.
return attrs[0].Value;
}
示例15: GetStringValue
/// <summary>
/// 列挙体に設定された文字列を取得する
/// </summary>
/// <param name="value">文字列が付加された列挙子</param>
/// <returns>設定された文字列</returns>
public static string GetStringValue(Enum value)
{
if (value == null) return null;
// すでにキャッシュしたものがあればそれを返す
if (m_Cashe.ContainsKey(value))
{
return m_Cashe[value];
}
// キャッシュになければ新しく取得
Type enumType = value.GetType();
string name = Enum.GetName(enumType, value);
StringEnumAttribute[] attributes =
(StringEnumAttribute[])enumType.GetField(name).GetCustomAttributes(typeof(StringEnumAttribute), false);
if (attributes.Length > 0)
{
// 属性があれば取得する
string stringValue = attributes[0].m_StringValue;
// キャッシュとして保存しておく
m_Cashe[value] = stringValue;
return stringValue;
}
else
{
// 属性がなければ無ければ空の値をキャッシュに保存してから返す
// キャッシュとして保存しておく
m_Cashe[value] = NoDataValue;
return NoDataValue;
}
}