0
任何人都知道什麼是最好的用來讀取XmlEnumAttributeGetMember VS在C#getfield命令性能
選擇1:GetMember
public static string XmlEnum(this Enum e)
{
Type type = e.GetType();
MemberInfo[] memInfo = type.GetMember(e.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(XmlEnumAttribute), false);
if (attrs != null && attrs.Length > 0)
{
return ((XmlEnumAttribute)attrs[0]).Name;
}
}
return e.ToString();
}
方案2:通過getfield命令
public static string XmlEnum2(this Enum e)
{
Type type = e.GetType();
FieldInfo info = type.GetField(e.ToString());
if (!info.IsDefined(typeof(XmlEnumAttribute), false))
{
return e.ToString();
}
object[] attrs = info.GetCustomAttributes(typeof(XmlEnumAttribute), false);
return ((XmlEnumAttribute)attrs[0]).Name;
}
爲什麼不嘗試10萬次,看看每次需要多長時間? – 2012-03-23 10:56:24
無論哪一個更快,都不可能在任何程序中產生可衡量的差異。 – 2012-03-23 11:14:25