c#测试反射性能示例

时间:2021-12-19 23:13:47 

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

标签:c#,反射
0
投稿

猜你喜欢

  • 深入了解SparkSQL的运用及方法

    2023-06-08 13:43:27
  • intellij idea修改maven配置时总是恢复默认配置的解决方法idea版本(2020.2.x)

    2023-06-30 23:56:10
  • Spring中的bean概念介绍

    2023-01-18 07:46:49
  • 浅谈Java中Collections.sort对List排序的两种方法

    2021-11-18 11:20:22
  • Java四种常用线程池的详细介绍

    2021-09-29 17:45:46
  • 基于JDK8总结java中的interrupt

    2021-12-27 07:43:54
  • Java实现雪花算法的原理和实战教程

    2021-11-20 18:20:41
  • Java并发编程同步器CountDownLatch

    2022-10-17 18:59:34
  • java基础学习笔记之泛型

    2022-05-31 01:49:44
  • 利用Java写一个学生管理系统

    2023-09-24 17:06:54
  • Android基于SoftReference缓存图片的方法

    2023-07-13 00:53:21
  • C#使用LINQ查询表达式的基本子句总结

    2022-08-05 05:09:41
  • C#在foreach遍历删除集合中元素的三种实现方法

    2022-05-28 12:22:50
  • Spring bean配置单例或多例模式方式

    2023-01-18 04:03:57
  • Spring Feign超时设置深入了解

    2022-01-17 10:39:06
  • Linux中Java开发常用软件安装方法总结

    2022-03-11 16:21:03
  • 六款值得推荐的android(安卓)开源框架简介

    2023-06-24 01:46:54
  • Java+Swing实现五子棋游戏的示例代码

    2022-06-27 11:56:22
  • 详解Spring Data Jpa 模糊查询的正确用法

    2022-03-28 15:07:50
  • MyBatis逆向⼯程的生成过程

    2023-11-28 17:29:22
  • asp之家 软件编程 m.aspxhome.com