欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#测试反射性能示例

程序员文章站 2024-02-23 13:46:16
activator.createinstance和assemblycreateinstance性能测试 复制代码 代码如下:using system;using syst...

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);
    }
}