本文整理汇总了C#中Enum.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# Enum.GetType方法的具体用法?C# Enum.GetType怎么用?C# Enum.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Enum
的用法示例。
在下文中一共展示了Enum.GetType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Parse
public static void Parse(string value, bool ignoreCase, Enum expected)
{
SimpleEnum result;
if (!ignoreCase)
{
Assert.True(Enum.TryParse(value, out result));
Assert.Equal(expected, result);
Assert.Equal(expected, Enum.Parse(expected.GetType(), value));
}
Assert.True(Enum.TryParse(value, ignoreCase, out result));
Assert.Equal(expected, result);
Assert.Equal(expected, Enum.Parse(expected.GetType(), value, ignoreCase));
}
示例2: GetStringValue
/// <summary>
/// Gets a string value for a particular enum value.
/// </summary>
/// <param name="value">Value.</param>
/// <returns>String Value associated via a <see cref="StringValueAttribute"/> attribute, or null if not found.</returns>
public static string GetStringValue(Enum value)
{
string output = null;
Type type = value.GetType();
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)
{
try
{
_stringValues.Add(value, attrs[0]);
}
catch { }
output = attrs[0].Value;
}
}
return output;
}
示例3: Get
public float Get(Enum statKey)
{
Type keyType = statKey.GetType();
Assert.IsTrue(stats.ContainsKey(keyType));
return stats[keyType].Get(statKey);
}
示例4: Find
Status2 Find(Enum statusKey)
{
Type keyType = statusKey.GetType();
if (statuses.ContainsKey(keyType))
{
return statuses[keyType];
}
return null;
}
示例5: EnumMemberConfiguration
/// <summary>
/// Initializes a new instance of the <see cref="EnumMemberConfiguration"/> class.
/// </summary>
/// <param name="member">The member of the enum type.</param>
/// <param name="declaringType">The declaring type of the member.</param>
public EnumMemberConfiguration(Enum member, EnumTypeConfiguration declaringType)
{
if (member == null)
{
throw Error.ArgumentNull("member");
}
if (declaringType == null)
{
throw Error.ArgumentNull("declaringType");
}
Contract.Assert(member.GetType() == declaringType.ClrType);
MemberInfo = member;
DeclaringType = declaringType;
AddedExplicitly = true;
_name = Enum.GetName(member.GetType(), member);
}
示例6: GetEnumDescription
public static string GetEnumDescription (Enum value)
{
FieldInfo fi = value.GetType ().GetField (value.ToString ());
DescriptionAttribute [] attributes =
(DescriptionAttribute []) fi.GetCustomAttributes (typeof (DescriptionAttribute), false);
if (attributes.Length > 0) {
return attributes [0].Description;
} else {
return value.ToString ();
}
}
示例7: GetDescription
public static string GetDescription(Enum key)
{
FieldInfo fi = key.GetType().GetField(key.ToString());
if (null != fi)
{
object[] attrs = fi.GetCustomAttributes(typeof(EnumDescriptionAttribute), true);
if (attrs != null && attrs.Length > 0)
return ((EnumDescriptionAttribute)attrs[0]).Description;
}
return string.Empty;
}
示例8: GetDescription
public static string GetDescription(Enum en)
{
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(Description),
false);
if (attrs != null && attrs.Length > 0)
return ((Description)attrs[0]).Text;
}
return en.ToString();
}
示例9: EnumToolbar
/// <summary>
/// Creates a toolbar that is filled in from an Enum. Useful for setting tool modes.
/// </summary>
public static Enum EnumToolbar(Enum selected)
{
string[] toolbar = System.Enum.GetNames(selected.GetType());
Array values = System.Enum.GetValues(selected.GetType());
for (int i=0; i < toolbar.Length; i++)
{
string toolname = toolbar[i];
toolname = toolname.Replace("_", " ");
toolbar[i] = toolname;
}
int selected_index = 0;
while (selected_index < values.Length)
{
if (selected.ToString() == values.GetValue(selected_index).ToString())
{
break;
}
selected_index++;
}
selected_index = GUILayout.Toolbar(selected_index, toolbar);
return (Enum) values.GetValue(selected_index);
}
示例10: GetStringValue
public static string GetStringValue(Enum value)
{
// Get the type
Type type = value.GetType();
// Get fieldinfo for this type
FieldInfo fieldInfo = type.GetField(value.ToString());
// Get the stringvalue attributes
StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
typeof(StringValueAttribute), false) as StringValueAttribute[];
// Return the first if there was a match.
return attribs.Length > 0 ? attribs[0].StringValue : null;
}
示例11: Get
public static string Get(Enum value)
{
string output = string.Empty;
Type type = value.GetType();
//Look for our 'StringValueAttribute'
//in the field's custom attributes
FieldInfo fi = type.GetField(value.ToString());
StringEnum[] attrs = fi.GetCustomAttributes(typeof(StringEnum), false) as StringEnum[];
if (attrs.Length > 0)
{
output = attrs[0].Value;
}
return output;
}
示例12: HasFlag
//.Net 3.5 version of the .Net 4.0 HasFlag http://stackoverflow.com/questions/4108828/generic-extension-method-to-see-if-an-enum-contains-a-flag
/// <summary>
/// Check to see if a flags enumeration has a specific flag set.
/// </summary>
/// <param name="variable">Flags enumeration to check</param>
/// <param name="value">Flag to check for</param>
/// <returns>Indicates if the flag is set.</returns>
public static bool HasFlag(this Enum variable, Enum value)
{
if (variable == null)
return false;
if (value == null)
throw new ArgumentNullException("value");
// Not as good as the .NET 4 version of this function, but should be good enough
if (!Enum.IsDefined(variable.GetType(), value))
{
throw new ArgumentException(string.Format(
"Enumeration type mismatch. The flag is of type '{0}', was expecting '{1}'.",
value.GetType(), variable.GetType()));
}
ulong num = Convert.ToUInt64(value);
return ((Convert.ToUInt64(variable) & num) == num);
}
示例13: GetFsmEventEnumValue
// // Update is called once per frame
// void Update () {
//
// }
public static string GetFsmEventEnumValue(Enum value)
{
string output = null;
Type type = value.GetType();
//Check first in our cached results...
//Look for our 'NGuiPlayMakerDelegates Attribute'
//in the field's custom attributes
FieldInfo fi = type.GetField(value.ToString());
PlayMakerUtils_FsmEvent[] attrs = fi.GetCustomAttributes(typeof(PlayMakerUtils_FsmEvent),false) as PlayMakerUtils_FsmEvent[];
if (attrs.Length > 0)
{
output = attrs[0].Value;
}
return output;
}
示例14: GetSelectList
public static IList<SelectListItem> GetSelectList(Type type, Enum value)
{
IList<SelectListItem> selectList = GetSelectList(type);
Type type2 = (value == 0) ? null : value.GetType();
if (((type2 != null) && (type2 != type)) && (type2 != Nullable.GetUnderlyingType(type)))
{
throw Error.Argument("value", MvcResources.EnumHelper_InvalidValueParameter, new object[] { type2.FullName, type.FullName });
}
if (((value == 0) && (selectList.Count != 0)) && string.IsNullOrEmpty(selectList[0].Value))
{
selectList[0].Selected = true;
return selectList;
}
string str = (value == 0) ? "0" : value.ToString("d");
bool flag = false;
for (int i = selectList.Count - 1; !flag && (i >= 0); i--)
{
SelectListItem item = selectList[i];
item.Selected = str == item.Value;
flag |= item.Selected;
}
if (!flag)
{
if ((selectList.Count != 0) && string.IsNullOrEmpty(selectList[0].Value))
{
selectList[0].Selected = true;
selectList[0].Value = str;
return selectList;
}
SelectListItem item2 = new SelectListItem {
Selected = true,
Text = string.Empty,
Value = str
};
selectList.Insert(0, item2);
}
return selectList;
}
示例15: HasFlag
public static bool HasFlag(this Enum e, Enum flag)
{
// Check whether the flag was given
if (flag == null)
{
throw new ArgumentNullException("flag");
}
// Compare the types of both enumerations
if (e.GetType() != (flag.GetType()))
{
throw new ArgumentException(string.Format(
"The type of the given flag is not of type {0}", e.GetType()),
"flag");
}
// Get the type code of the enumeration
var typeCode = e.GetTypeCode();
// If the underlying type of the flag is signed
if (typeCode == TypeCode.SByte || typeCode == TypeCode.Int16 || typeCode == TypeCode.Int32 ||
typeCode == TypeCode.Int64)
{
return (Convert.ToInt64(e) & Convert.ToInt64(flag)) != 0;
}
// If the underlying type of the flag is unsigned
if (typeCode == TypeCode.Byte || typeCode == TypeCode.UInt16 || typeCode == TypeCode.UInt32 ||
typeCode == TypeCode.UInt64)
{
return (Convert.ToUInt64(e) & Convert.ToUInt64(flag)) != 0;
}
// Unsupported flag type
throw new Exception(string.Format("The comparison of the type {0} is not implemented.", e.GetType().Name));
}