zl程序教程

您现在的位置是:首页 >  后端

当前栏目

c#测试反射性能示例

c#测试性能反射 测试 C# 示例 性能
2023-06-13 09:15:24 时间

Activator.CreateInstance和AssemblyCreateInstance性能测试

复制代码代码如下:


usingSystem;
usingSystem.Collections.Generic;
usingSystem.Diagnostics;
usingSystem.Linq;
usingSystem.Security.Cryptography;
usingSystem.Text;
usingHelloWorld.ServiceReference1;
usingSystem.Globalization;
usingSystem.Reflection;
usingInterface;

namespaceHelloWorld
{
   classProgram
   {
       staticvoidMain(string[]args)
       {
           Stopwatchs=newStopwatch();
           Assemblya=Assembly.GetExecutingAssembly();

           //foreach(varstina.GetExportedTypes())
           //{
           //   Console.WriteLine(st.Name);
           //}

           s.Reset();
           s.Start();
           Typet=a.GetType("HelloWorld.Test1");
           Interface1i2=(Interface1)(Activator.CreateInstance(t));
           Console.WriteLine((i2.Add(1,2)));
           s.Stop();
           Console.WriteLine(s.Elapsed);


           s.Reset();
           s.Start();
           Interface1i=(Interface1)a.CreateInstance("HelloWorld.Test1");
           Console.WriteLine((i.Add(1,2)));
           s.Stop();
           Console.WriteLine(s.Elapsed);

       }
   }
   publicclassTest1:Interface1
   {
       publicintAdd(inta,intb)
       {
           returna+b;
       }
   }
   publicinterfaceInterface1
   {
       intAdd(inta,intb);
   }
}