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