本文整理汇总了C#中Enum.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# Enum.ToString方法的具体用法?C# Enum.ToString怎么用?C# Enum.ToString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Enum
的用法示例。
在下文中一共展示了Enum.ToString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetEnumDescription
/// <summary>
/// Gets the Description of the given Enumeration value
/// </summary>
/// <param name="value">The enumeration value</param>
/// <returns>The Description from the DescriptionAttribute attached to the value, otherwise the enumeration value's name</returns>
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 ();
}
}
示例2: 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();
}
示例3: 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;
}
示例4: ApplyBuff
float ApplyBuff(Enum statType, float value)
{
string strStatType = statType.ToString();
float addSum = 0;
float multiSum = 0;
foreach (var buff in buffs)
{
if (buff.Value.statMod != null)
{
foreach (var buffStat in buff.Value.statMod)
{
string strBuffStatType = buffStat.Key.ToString();
if (strBuffStatType == strStatType)
{
addSum += buffStat.Value;
}
else if (strBuffStatType != "GoldGetAmplifier" && strBuffStatType == strStatType + "Amplifier")
{
multiSum += buffStat.Value;
}
}
}
}
return (value + addSum) * (1 + multiSum);
}
示例5: Limp_Enter
IEnumerator Limp_Enter(Enum prevState)
{
Debug.Log("Enter Limp " + prevState.ToString());
yield return new WaitForSeconds(5);
Debug.Log("Limp Entered");
}
示例6: RemoveState
public void RemoveState(Enum stateId) {
FSMState state = GetState(stateId);
if(state == null)
throw new Exception("Could not find state " + stateId.ToString() + " for removal.");
_states.Remove(state);
}
示例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: GetAnchorPosition
/// <summary>
/// Looks thought the list of all the UI anchors, then grabs it based on the enum
/// </summary>
/// <param name="anchorToGet">The Enum to get </param>
/// <returns>The UI GameObject</returns>
public Vector3 GetAnchorPosition(Enum.Anchors anchorToGet)
{
try
{
GameObject obj = Anchors.Where(x => x.gameObject.name == anchorToGet.ToString()).SingleOrDefault();
if (obj == null)
throw new System.Exception("Didn't find an object in list");
return obj.transform.position;
}
catch (System.Exception ex)
{
Debug.LogError("[SELF] Couldn't find Anchor in List, Make sure the GameObject and Enum are name exactly the SAME, because it compares strings, error: " + ex.Message + " returning Vector3.Zero");
return Vector3.zero;
}
}
示例9: 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;
}
示例10: 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;
}
示例11: 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;
}
示例12: 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;
}
示例13: RemoveMember
/// <summary>
/// Removes the given member.
/// </summary>
/// <param name="member">The member being removed.</param>
public void RemoveMember(Enum member)
{
if (member == null)
{
throw Error.ArgumentNull("member");
}
if (member.GetType() != ClrType)
{
throw Error.Argument("member", SRResources.PropertyDoesNotBelongToType, member.ToString(), ClrType.FullName);
}
if (ExplicitMembers.ContainsKey(member))
{
ExplicitMembers.Remove(member);
}
if (!RemovedMembers.Contains(member))
{
RemovedMembers.Add(member);
}
}
示例14: GetDescription
// Credits: http://www.codeproject.com/KB/cs/enumwithdescription.aspx?msg=649306#xx649306xx
private string GetDescription(Enum e)
{
FieldInfo fi = e.GetType().GetField(e.ToString());
DescriptionAttribute[] da = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return (da.Length > 0) ? da[0].Description : e.ToString();
}
示例15: AddMember
/// <summary>
/// Adds an enum member to this enum type.
/// </summary>
/// <param name="member">The member being added.</param>
/// <returns>The <see cref="EnumMemberConfiguration"/> so that the member can be configured further.</returns>
public EnumMemberConfiguration AddMember(Enum member)
{
if (member == null)
{
throw Error.ArgumentNull("member");
}
if (member.GetType() != ClrType)
{
throw Error.Argument("member", SRResources.PropertyDoesNotBelongToType, member.ToString(), ClrType.FullName);
}
// Remove from the ignored members
if (RemovedMembers.Contains(member))
{
RemovedMembers.Remove(member);
}
EnumMemberConfiguration memberConfiguration;
if (ExplicitMembers.ContainsKey(member))
{
memberConfiguration = ExplicitMembers[member];
}
else
{
memberConfiguration = new EnumMemberConfiguration(member, this);
ExplicitMembers[member] = memberConfiguration;
}
return memberConfiguration;
}