记得老早以前就看到有人把反射称之为“性能杀手",正好目前正在做的erp系统为了实现扩展,系统就是架构在反射的基础之上,数据交互也都是通过反
射来实现。首先系统会产生太多dll,现在刚刚起步,已经高达43个之多,由于erp系统具有很复杂的业务功能,产品在发布阶段很可能产生200甚至更多
的dll,且不说这几百个项目在调试的时候,漫长的等待是多么的痛苦,在产品推出去之后,这么多的dll的版本如何维护?多现在为止,除了windows
还没有见过dll超过百的系统,难道我们的系统真的比windows还要难?这些暂且不提,先测试一下反射在系统应用的性能情况,测试程序如下:
//被测试类
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassLibrary1
{
public class CTester
{
public CTester()
{
a = 10;
}
public void test1()
{
a = (a - 0.0001) * 1.0001;
}
private double a;
public double geta() { return a; }
}
}
///测试类
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Collections;
namespace ConsoleApplication2
{
public class CTester
{
public CTester()
{
a = 10;
}
public void test1()
{
a = (a - 0.0001) * 1.0001;
}
private double a;
public double geta() { return a; }
}
class Program
{
private string test1()
{
int now = System.Environment.TickCount;
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 100; j++)
{
CTester aTest = new CTester();
aTest.test1();
}
}
string time = (System.Environment.TickCount - now).ToString();
return time;
}
static Hashtable table = new Hashtable();
private string test3()
{
int now = System.Environment.TickCount;
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 100; j++)
{
Type theTest = null;
if (table.ContainsKey("ConsoleApplication2.CTester"))
{
theTest = table["ConsoleApplication2.CTester"] as Type;
}
else
{
theTest = Type.GetType("ConsoleApplication2.CTester");
table.Add("ConsoleApplication2.CTester", theTest);
}
ConsoleApplication2.CTester theobj = theTest.InvokeMember(null, BindingFlags.CreateInstance
, null, null, null) as ConsoleApplication2.CTester;
theobj.test1();
}
}
string time =(System.Environment.TickCount - now).ToString();
return time;
}
private string test2()
{
int now = System.Environment.TickCount;
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 100; j++)
{
Type theTest = theTest = Type.GetType("ConsoleApplication2.CTester"); ;
ConsoleApplication2.CTester theobj = theTest.InvokeMember(null, BindingFlags.CreateInstance
, null, null, null) as ConsoleApplication2.CTester;
theobj.test1();
}
}
string time =(System.Environment.TickCount - now).ToString();
return time;
}
static object InvokeMehod(string FileName,
string MethodName,
object[] Args)
{
Assembly a = Assembly.LoadFrom(FileName);
MethodInfo method = null;
Type HereType = null;
foreach (Module m in a.GetModules())
{
foreach (Type t in m.GetTypes())
{
foreach (MethodInfo mInfo in t.GetMethods())
{
if (mInfo.Name == MethodName)
{
ParameterInfo[] pInfo = mInfo.GetParameters();
if (pInfo.Length == Args.Length)
{
bool same = true;
for (int i = 0; i < pInfo.Length; i++)
{
if (Args[i] != null && pInfo[i].ParameterType != typeof(object)
&& pInfo[i].ParameterType != typeof(object[]))
{
if (pInfo[i].ParameterType != Args[i].GetType())
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassLibrary1
{
public class CTester
{
public CTester()
{
a = 10;
}
public void test1()
{
a = (a - 0.0001) * 1.0001;
}
private double a;
public double geta() { return a; }
}
}
///测试类
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Collections;
namespace ConsoleApplication2
{
public class CTester
{
public CTester()
{
a = 10;
}
public void test1()
{
a = (a - 0.0001) * 1.0001;
}
private double a;
public double geta() { return a; }
}
class Program
{
private string test1()
{
int now = System.Environment.TickCount;
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 100; j++)
{
CTester aTest = new CTester();
aTest.test1();
}
}
string time = (System.Environment.TickCount - now).ToString();
return time;
}
static Hashtable table = new Hashtable();
private string test3()
{
int now = System.Environment.TickCount;
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 100; j++)
{
Type theTest = null;
if (table.ContainsKey("ConsoleApplication2.CTester"))
{
theTest = table["ConsoleApplication2.CTester"] as Type;
}
else
{
theTest = Type.GetType("ConsoleApplication2.CTester");
table.Add("ConsoleApplication2.CTester", theTest);
}
ConsoleApplication2.CTester theobj = theTest.InvokeMember(null, BindingFlags.CreateInstance
, null, null, null) as ConsoleApplication2.CTester;
theobj.test1();
}
}
string time =(System.Environment.TickCount - now).ToString();
return time;
}
private string test2()
{
int now = System.Environment.TickCount;
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 100; j++)
{
Type theTest = theTest = Type.GetType("ConsoleApplication2.CTester"); ;
ConsoleApplication2.CTester theobj = theTest.InvokeMember(null, BindingFlags.CreateInstance
, null, null, null) as ConsoleApplication2.CTester;
theobj.test1();
}
}
string time =(System.Environment.TickCount - now).ToString();
return time;
}
static object InvokeMehod(string FileName,
string MethodName,
object[] Args)
{
Assembly a = Assembly.LoadFrom(FileName);
MethodInfo method = null;
Type HereType = null;
foreach (Module m in a.GetModules())
{
foreach (Type t in m.GetTypes())
{
foreach (MethodInfo mInfo in t.GetMethods())
{
if (mInfo.Name == MethodName)
{
ParameterInfo[] pInfo = mInfo.GetParameters();
if (pInfo.Length == Args.Length)
{
bool same = true;
for (int i = 0; i < pInfo.Length; i++)
{
if (Args[i] != null && pInfo[i].ParameterType != typeof(object)
&& pInfo[i].ParameterType != typeof(object[]))
{
if (pInfo[i].ParameterType != Args[i].GetType())