c# 利用反射来创建实例

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Reflection;
6
7 namespace ThreadDemo
8 {
9    /** <summary>
10    /// 反射实例
11    /// </summary>
12    class Reflection
13    {
14        static void Main(string[] args)
15        {
16            Assembly asm = Assembly.Load("ThreadDemo");//加载程序集
17            Type[] typeInfo = asm.GetTypes();//获取程序集类型数组
18            foreach (Type types in typeInfo)
19            {
20                Console.WriteLine("程序集中类型:{0}", types.Name);
21                MethodInfo[] mInfo = types.GetMethods();
22                foreach (MethodInfo information in mInfo)
23                {
24                    Console.WriteLine("此类型所有的方法:{0}", information.Name);
25                }
26                Console.WriteLine("\n");
27            }
28            Console.WriteLine("\n\n");
29            Console.WriteLine("开始!");
30
31            Type inType = typeof(ThreadTest);
32            object obj = Activator.CreateInstance(inType);
33            string[] s = { "mlg" };//方法的参数
34            object objName = Activator.CreateInstance(inType, s);//创建指定类型的实例
35
36            MethodInfo methodInfo = inType.GetMethod("ThreadMethod");//根据字符串来获取方法信息
37            // methodInfo.Invoke(obj, null);
38            methodInfo.Invoke(objName, null);//调用类型中的方法
39            Console.ReadLine();
40
41        }
42    }
43}
44

45

//------------------------------------------------------------------------------------------------

1 namespace ThreadDemo
2 {
3     /// <summary>
4     /// 委托
5     /// </summary>
6     class Delegate
7     {
8         public delegate int testDelegate(int i,int j);//定义委托类型
9         static void Main(string[] args)
10         {
11             Delegate d = new Delegate();
12             d.DataInfo();
13         }
14         public void getDeltegate(testDelegate dg,int i,int j)//委托类型的传参
15         {
16             Console.WriteLine(dg(i,j).ToString());//调用被委托的方法
17             Console.ReadLine();
18         }
19         /// <summary>
20         /// 动态调用委托
21         /// </summary>
22         public void DataInfo()
23         {
24           //调用委托
25             getDeltegate(new testDelegate(DelegateInfor),2,3);
26             getDeltegate(new testDelegate(DelegateInfor2),1,1);
27             getDeltegate(new testDelegate(DelegateInfor3),-1,-1);
28         }
29 
30         public int DelegateInfor(int i,int j)
31         {
32             return i+j;
33         }
34         public int DelegateInfor2(int i, int j)
35         {
36             return i + j;
37         }
38         public int DelegateInfor3(int i, int j)
39         {
40             return i + j;
41         }
42   }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值