![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public
void
testperformance()
{
string assembly = " Pixysoft.Framework.Noebe.Orm " ;
string type = " Pixysoft.Framework.Noebe.Orm.testdriven.Pojo " ;
CodeTimer.Initialize();
CodeTimer.Time( " reflection " , 100000 , delegate
{
Type _type = Assembly.Load(assembly).GetType(type);
Object obj = Activator.CreateInstance(_type);
});
CodeTimer.Time( " direct " , 100000 , delegate
{
Object obj = new Pojo();
});
CodeTimer.Time( " dynamic " , 100000 , delegate
{
Type _type = Assembly.Load(assembly).GetType(type);
Object obj = ReflectionManager.CreateDynamicType(_type).GetDefaultConstructor().Invoke();
});
}
{
string assembly = " Pixysoft.Framework.Noebe.Orm " ;
string type = " Pixysoft.Framework.Noebe.Orm.testdriven.Pojo " ;
CodeTimer.Initialize();
CodeTimer.Time( " reflection " , 100000 , delegate
{
Type _type = Assembly.Load(assembly).GetType(type);
Object obj = Activator.CreateInstance(_type);
});
CodeTimer.Time( " direct " , 100000 , delegate
{
Object obj = new Pojo();
});
CodeTimer.Time( " dynamic " , 100000 , delegate
{
Type _type = Assembly.Load(assembly).GetType(type);
Object obj = ReflectionManager.CreateDynamicType(_type).GetDefaultConstructor().Invoke();
});
}
------ Test started: Assembly: Pixysoft.Framework.Noebe.Orm.dll ------
reflection
Time Elapsed: 2,422ms
CPU time: 2,421,875,000ns
Gen 0: 19
Gen 1: 0
Gen 2: 0
direct
Time Elapsed: 1ms
CPU time: 0ns
Gen 0: 1
Gen 1: 0
Gen 2: 0
dynamic
Time Elapsed: 2,726ms
CPU time: 2,718,750,000ns
Gen 0: 91
Gen 1: 0
Gen 2: 0
1 passed, 0 failed, 0 skipped, took 5.41 seconds (Ad hoc).
结论:
1. 反射创建对象的开销基本上可以忽略了。
2. 自己使用动态等方法创建对象并没有很大提高性能。
3. 反射创建对象的唯一难点在 参考插件里面出现的问题:加载搜索+多dll版本内存重复。