当前位置: 移动技术网 > IT编程>开发语言>.net > .NET获取枚举DescriptionAttribute描述信息性能改进的多种方法

.NET获取枚举DescriptionAttribute描述信息性能改进的多种方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

十二之天贰双开,中文音译歌词,广州公交网查询

一. descriptionattribute的普通使用方式

1.1 使用示例

  descriptionattribute特性可以用到很多地方,比较常见的就是枚举,通过获取枚举上定义的描述信息在ui上显示,一个简单的枚举定义:

public enum enumgender
{
none,
[system.componentmodel.description("男")]
male,
[system.componentmodel.description("女")]
female,
other,
} 

  本文不讨论descriptionattribute的其他应用场景,也不关注多语言的实现,只单纯的研究下获取枚举描述信息的方法。

  一般比较常见的获取枚举描述信息的方法如下,可以在园子里搜索类似的代码非常多。

public static string getdescriptionoriginal(this enum @this)
{
var name = @this.tostring();
var field = @this.gettype().getfield(name);
if (field == null) return name;
var att = system.attribute.getcustomattribute(field, typeof(descriptionattribute), false);
return att == null ? field.name : ((descriptionattribute)att).description;
}

  简单测试下:

console.writeline(enumgender.female.getdescriptionoriginal());
console.writeline(enumgender.male.getdescriptionoriginal());
console.writeline(enumgender.other.getdescriptionoriginal()); //输出结果: 
女 
男 
other

1.2 上面的实现代码的问题

  首先要理解特性是什么?

特性:

attribute特性就是关联了一个目标对象的一段配置信息,存储在dll内的元数据。它本身没什么意义,可以通过反射来获取配置的特性信息。

  因此主要问题其实就是反射造成的严重性能问题:

•1.每次调用都会使用反射,效率慢!
•2.每次调用反射都会生成新的descriptionattribute对象,哪怕是同一个枚举值。造成内存、gc的极大浪费!
•3.好像不支持位域组合对象!
•4.这个地方的方法参数是enum,enum是枚举的基类,他是一个引用类型,而枚举是值类型,该方法会造成装箱,不过这个问题好像是不可避免的。

  性能到底有多差呢?代码来实测一下:

[test]
public void getdescriptionoriginal_test()
{
var enums = this.gettestenums();
console.writeline(enums.count);
testhelper.invokeandwriteall(() =>
{
system.threading.tasks.parallel.for(0, 1000000, (i, obj) =>
{
foreach (var item in enums)
{
var a = item.getdescriptionoriginal();
}
});
});
}
//输出结果:
80
timespan:79,881.0000ms //共消耗了将近80秒
memoryused:-1,652.7970kb
collectioncount(0):7,990.00 //0代gc回收了7千多次,因为创建了大量的descriptionattribute对象 

  其中this.gettestenums();方法使用获取一个枚举值集合,用于测试的,集合大小80,执行100w次,相当于执行了8000w次getdescriptionoriginal方法。

  testhelper.invokeandwriteall方法是用来计算执行前后的时间、内存消耗、0代gc回收次数的,文末附录中给出了代码,由于内存回收的原因,内存消耗计算其实不准确的,不过可以参考第三个指标0代gc回收次数。

二. 改进的descriptionattribute方法

  知道了问题原因,解决就好办了,基本思路就是把获取到的文本值缓存起来,一个枚举值只反射一次,这样性能问题就解决了。

2.1 使用字典缓存+锁

  因为使用静态变量字典来缓存值,就涉及到线程安全,需要使用锁(做了双重检测),具体方法:

private static dictionary<enum, string> _lockdictionary = new dictionary<enum, string>();
public static string getdescriptionbydictionarywithlocak(this enum @this)
{
if (_lockdictionary.containskey(@this)) return _lockdictionary[@this];
monitor.enter(_obj);
if (!_lockdictionary.containskey(@this))
{
var value = @this.getdescriptionoriginal();
_lockdictionary.add(@this, value);
}
monitor.exit(_obj);
return _lockdictionary[@this];
} 

  来测试一下,测试数据、次数和1.2的getdescriptionoriginal_test相同,效率有很大的提升,只有一次内存回收。

[test]
public void getdescriptionbydictionarywithlocak_test()
{
var enums = this.gettestenums();
console.writeline(enums.count)
testhelper.invokeandwriteall(() =>
{
system.threading.tasks.parallel.for(0, 1000000, (i, obj) =>
{
foreach (var item in enums)
{
var a = item.getdescriptionbydictionarywithlocak();
}
});
});
}
//测试结果:
80
timespan:1,860.0000ms
memoryused:159.2422kb
collectioncount(0):1.00 

2.2 使用字典缓存+异常(不走寻常路的方式)

  还是先看看实现方法吧!

private static dictionary<enum, string> _exceptiondictionary = new dictionary<enum, string>();
public static string getdescriptionbydictionarywithexception(this enum @this)
{
try
{
return _exceptiondictionary[@this];
}
catch (keynotfoundexception)
{
monitor.enter(_obj);
if (!_exceptiondictionary.containskey(@this))
{
var value = @this.getdescriptionoriginal();
_exceptiondictionary.add(@this, value);
}
monitor.exit(_obj);
return _exceptiondictionary[@this];
}
}

  假设我们的使用场景是这样的:项目定义的枚举并不多,但是用其描述值很频繁,比如定义了一个用户性别枚举,用的地方很多,使用频率很高。

  上面getdescriptionbydictionarywithlocak的方法中,第一句代码“if (_lockdictionary.containskey(@this)) ”就是验证是否包含枚举值。在2.1的测试中执行了8000w次,其中只有80次(总共只有80个枚举值用于测试)需要这句代码“if (_lockdictionary.containskey(@this)) ”,其余的直接取值就可了。基于这样的考虑,就有了上面的方法getdescriptionbydictionarywithexception。

  来测试一下,看看效果吧!

[test]
public void getdescriptionbydictionarywithexception_test()
{
var enums = this.gettestenums();
console.writeline(enums.count);
testhelper.invokeandwriteall(() =>
{
system.threading.tasks.parallel.for(0, 1000000, (i, obj) =>
{
foreach (var item in enums)
{
var a = item.getdescriptionbydictionarywithexception();
}
});
});
}
//测试结果:
80
timespan:1,208.0000ms
memoryused:230.9453kb
collectioncount(0):1.00

  测试结果来看,基本上差不多,在时间上略微快乐一点点,1,208.0000ms:1,860.0000ms,执行8000w次快600毫秒,好像差别也不大啊,这是为什么呢?

  这个其实就是dictionary的问题了,dictionary内部使用散列算法计算存储地址,其查找的时间复杂度为o(1),他的查找效果是非常快的,而本方法中利用了异常处理,异常捕获本身是有一定性能影响的。

2.3 推荐简单方案:concurrentdictionary

  concurrentdictionary是一个线程安全的字典类,代码:

private static concurrentdictionary<enum, string> _concurrentdictionary = new concurrentdictionary<enum, string>();
public static string getdescriptionbyconcurrentdictionary(this enum @this)
{
return _concurrentdictionary.getoradd(@this, (key) =>
{
var type = key.gettype();
var field = type.getfield(key.tostring());
return field == null ? key.tostring() : getdescription(field);
});
}

  测试代码及测试结果:

[test]
public void getdescriptionbyconcurrentdictionary_test()
{
var enums = this.gettestenums();
console.writeline(enums.count);
testhelper.invokeandwriteall(() =>
{
system.threading.tasks.parallel.for(0, 1000000, (i, obj) =>
{
foreach (var item in enums)
{
var a = item.getdescriptionbyconcurrentdictionary();
}
});
});
}
//测试结果:
80
timespan:1,303.0000ms
memoryused:198.0859kb
collectioncount(0):1.00 

2.4 正式的代码

  综上所述,解决了性能问题、位域枚举问题的正式的代码:

/// <summary>
/// 获取枚举的描述信息(descripion)。
/// 支持位域,如果是位域组合值,多个按分隔符组合。
/// </summary>
public static string getdescription(this enum @this)
{
return _concurrentdictionary.getoradd(@this, (key) =>
{
var type = key.gettype();
var field = type.getfield(key.tostring());
//如果field为null则应该是组合位域值,
return field == null ? key.getdescriptions() : getdescription(field);
});
}
/// <summary>
/// 获取位域枚举的描述,多个按分隔符组合
/// </summary>
public static string getdescriptions(this enum @this, string separator = ",")
{
var names = @this.tostring().split(',');
string[] res = new string[names.length];
var type = @this.gettype();
for (int i = 0; i < names.length; i++)
{
var field = type.getfield(names[i].trim());
if (field == null) continue;
res[i] = getdescription(field);
}
return string.join(separator, res);
}
private static string getdescription(fieldinfo field)
{
var att = system.attribute.getcustomattribute(field, typeof(descriptionattribute), false);
return att == null ? field.name : ((descriptionattribute)att).description;
}

ps:.net获取枚举值的描述

一、给枚举值定义描述的方式

public enum timeofday 
{ 
[description("早晨")] 
moning = 1, 
[description("下午")] 
afternoon = 2, 
[description("晚上")] 
evening = 3, 
} 

二、获取枚举值的描述的方法

public static string getdescriptionfromenumvalue(type enumtype, object enumvalue)
{
try
{
object o = enum.parse(enumtype, enumvalue.tostring());
string name = o.tostring();
descriptionattribute[] customattributes = (descriptionattribute[])enumtype.getfield(name).getcustomattributes(typeof(descriptionattribute), false);
if ((customattributes != null) && (customattributes.length == 1))
{
return customattributes[0].description;
}
return name;
}
catch
{
return "未知";
}
}

三、获取枚举值的描述的方法的使用

string strmoning = getdescriptionfromenumvalue( typeof (timeofday) , 2 );

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网