C#中的RTTI和反射机制

以下觉有超详细注释。

第一个类:

using System;
using System.Collections.Generic;
using System.Text;

namespace Run
{
    class reflection
    {
        int x;
        int y;
        public reflection(int i)
        {
            Console.WriteLine("Constructing Reflection(int).");
            x = y = i;
            show();
        }
        public reflection(int i, int j)
        {
            Console.WriteLine("Constrcuting Reflection(int ,int).");
            x = i;
            y = j;
            show();
        }
        public int sum()
        {
            return x + y;
        }
        public bool isBetween(int i)
        {
            if ((x < i) && (i < y))
                return true;
            else
                return false;
        }
        public void set(int a, int b)
        {
            Console.WriteLine("Inside set(int ,int).");
            x = a;
            y = b;
            show();
        }
        public void show()
        {
            Console.WriteLine("Value are x:{0},y:{1}", x, y);
        }
    }
    class AnotherClass 
    {
        string remark;
        public AnotherClass(string str)
        {
            remark = str;
        }
        public void show()
        {
            Console.WriteLine(remark);
        }
    }
    class Demo
    {
        public static void Main()
        {
            Console.WriteLine("This is a placeholder.");
        }
    }
}


运行生成 "runs.exe" 文件

第二个类(进行发射的类):

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace Run
{
    class ReflectionTest
    {
        public static void Main()
        {
            int val;
            Assembly asm = Assembly.LoadFrom("runs.exe");   //获取程序集的一个实例,加载run.exe
            Type[] alltypes = asm.GetTypes();               //获取程序集中的类信息。
            foreach (Type temp in alltypes)                
                Console.WriteLine("Found:" + temp.Name);    //输出三个类名子
               
            Console.WriteLine();

            Type t = alltypes[0];                           
            Console.WriteLine("Using :" + t.Name);          
            ConstructorInfo[] ci = t.GetConstructors();     
            Console.WriteLine("输出构造函数列表:");   

            foreach (ConstructorInfo c in ci)
            {
                Console.WriteLine(" " + t.Name + "(");      
                ParameterInfo[] pi = c.GetParameters();
                for (int i = 0; i < pi.Length; i++)
                {
                    Console.WriteLine(pi[i].ParameterType.Name + " " +pi[i].Name );  //参数名+类型名
                    if (i + 1 < pi.Length)
                        Console.WriteLine(", ");
                }
                Console.WriteLine(") ");
            }
            Console.WriteLine(" ");

            int x;
            for( x = 0; x < ci.Length; x++ )                 //查找构造函数中参数等于2的
            {
                ParameterInfo[] pi = ci[x].GetParameters();  //获取构造函数参数信息
                if( pi.Length == 2)                          
                    break;
            }
            if (x == ci.Length)                              //如果x循环完,还没有发现构造函数 则会等于 ci.length 的值
            {
                Console.WriteLine("没有任何构造函数.");
                return;
            }
            else
            {
                Console.WriteLine("发现两个参数的构造函数./n");
            }
            object[] consarge = new object[2];                 
            consarge[0] = 10;
            consarge[1] = 20;
            object reflectOb = ci[x].Invoke(consarge);       //根据找到的两个参数的构造函数的数组索引 来进行调用

            Console.WriteLine("/n调用reflectOb方法.");
            Console.WriteLine();
            MethodInfo[] mi = t.GetMethods();                //获取t的方法信息

            foreach (MethodInfo m in mi)                     
            {
                ParameterInfo[] pi = m.GetParameters();

                if (m.Name.CompareTo("set") == 0 && pi[0].ParameterType == typeof(int))   //如果参数名 是 set,并且 参数类型是int
                {
                    object[] args = new object[2];
                    args[0] = 9;
                    args[1] = 10;
                    m.Invoke(reflectOb, args);             //装箱调用(对象| 参数)
                }
                else if (m.Name.CompareTo("set") == 0 && pi[0].ParameterType == typeof(double))
                {
                    object[] args = new object[2];
                    args[0] = 1.1;
                    args[1] = 23.4;
                    m.Invoke(reflectOb, args);         
                }
                else if (m.Name.CompareTo("sum") == 0)     //CompareTo的返回值是bool类型
                {
                    val = (int)m.Invoke(reflectOb, null);
                    Console.WriteLine("sum is:" + val); ;
                }
                else if (m.Name.CompareTo("isBetween") == 0)
                {
                    object[] args = new object[1];
                    args[0] = 14;
                    if ((bool)m.Invoke(reflectOb, args))
                        Console.WriteLine("14 是isBetween传递的参数。");
                }
                else if (m.Name.CompareTo("show") == 0)
                {
                    m.Invoke(reflectOb, null);
                }
            }
        }
    }
}
/* 输出   
Found:reflection
Found:AnotherClass
Found:Demo

Using :reflection
Available constructors:
 reflection(
Int32 i
)
 reflection(
Int32 i
,
Int32 j
)

发现两个参数的构造函数.

Constrcuting Reflection(int ,int).
Value are x:10,y:20

调用reflectOb方法.

sum is:30
14 是isBetween传递的参数
Inside set(int ,int).     //该顺序是根据遍历的reflection类的顺序输出的。
Value are x:9,y:10
Value are x:9,y:10
请按任意键继续. . .
*/


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值