Activator.CreateInstance和AssemblyCreateInstance性能测试
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);
}
}
新闻热点
疑难解答