c#测试反射性能示例
activator.createinstance和assemblycreateinstance性能测试
using system;
using system.collections.generic;
using system.diagnostics;
using system.linq;
using system.security.cryptography;
using system.text;
using helloworld.servicereference1;
using system.globalization;
using system.reflection;
using interface;
namespace helloworld
{
class program
{
static void main(string[] args)
{
stopwatch s = new stopwatch();
assembly a = assembly.getexecutingassembly();
//foreach (var st in a.getexportedtypes())
//{
// console.writeline(st.name);
//}
s.reset();
s.start();
type t = a.gettype("helloworld.test1");
interface1 i2 = (interface1)(activator.createinstance(t));
console.writeline((i2.add(1, 2)));
s.stop();
console.writeline(s.elapsed);
s.reset();
s.start();
interface1 i = (interface1)a.createinstance("helloworld.test1");
console.writeline((i.add(1, 2)));
s.stop();
console.writeline(s.elapsed);
}
}
public class test1 : interface1
{
public int add(int a, int b)
{
return a + b;
}
}
public interface interface1
{
int add(int a, int b);
}
}