如何獲取屬性[Describtion]信息
1 可以 通過:
using UnityEngine;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
public enum MyEnum
{
name1 = 1 ,
[Description("Here is another")]
HereIsAnother = 2,
[Description("Last -- -- one")]
LastOne = 3
}
public class EnumTest : MonoBehaviour {
void Start () {
Debug.Log(GetEnumDescription(MyEnum.LastOne));//通過枚舉值獲取值。
int value = 2 ;
Debug.Log(GetEnumDescription((MyEnum)value));//What I want to do is if I know the enum value (e.g. 1) - how can I retrieve the description? In //other words, how can I convert an integer into an "Enum value" to pass to my GetDescription method?可以 通過數(shù)字獲取該表述唆阿。
//The default underlying data type for anenumin C# is anint, you can just cast it. 在c#下 默認(rèn)是潛在值是 int .}
public static string GetEnumDescription(MyEnum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null &&
attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
}
Debug.Log() ; ?Last -- -- one
Debug.Log();?Here is another