这个Blog很强大

导航

c# 类的反射实例

Class1和Form 窗体在同一个命名空间
using System;
using System.Collections.Generic;
using System.Text;

namespace fanshetest1
{
    class Class1
    {
        private string ab="1";
        public Class1(string aa)
        {
            a = aa;
        }
        public int  aa(int x,int y)
        {
            return x+y+x+y;
        }
        public string bb()
        {
            return "bb";
        }
        public static string cc()
        {
            return "cc";
        }
        public string AB
        {
            get
            {
                return ab;
            }
            set
            {
                ab = value;
            }
        }
        public string a;
    }
}

Class1和Form 窗体在不同一个命名空间
using System;
using System.Collections.Generic;
using System.Text;

namespace fanshetest1
{
    class Class1
    {
        private string ab="1";
        public Class1(string aa)
        {
            a = aa;
        }
        public int  aa(int x,int y)
        {
            return x+y+x+y;
        }
        public string bb()
        {
            return "bb";
        }
        public static string cc()
        {
            return "cc";
        }
        public string AB
        {
            get
            {
                return ab;
            }
            set
            {
                ab = value;
            }
        }
        public string a;
    }
}
下面是如何使用反射操作以上类;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace fanshetest
{
    public partial class Form1 : Form
    {
        System.Reflection.Assembly a;
        System.Reflection.Assembly b;
        public Form1()
        {
            InitializeComponent();
            a = System.Reflection.Assembly.LoadFrom("Class1.DLL");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            gouzaohanshu();
          

        }
       //没有传参数,返回一个类型;
        private void One()
        {
            //再另建一个项目,在项目中引用上面生成的ClassLibrary1.DLL
            System.Type t = a.GetType("Class1.Class1");
            //动态生成ClassLibrary1.Class类的实例
            Object theObj = System.Activator.CreateInstance(t);
            //参数信息,GetSum需要两个string参数
            System.Type[] paramTypes = new System.Type[2];
            paramTypes[0] = System.Type.GetType("System.String");
            paramTypes[1] = System.Type.GetType("System.String");
            //System.Reflection.MethodInfo mi = t.GetMethod("aa", paramTypes);
            System.Reflection.MethodInfo mi = t.GetMethod("bb");
            //参数值
            Object[] parameters = new Object[2];
            parameters[0] = 3;
            parameters[1] = 4;

            Object returnValue = mi.Invoke(theObj, null);
            this.textBox1.Text = returnValue.ToString();
        }
        //没有返回值,调用的是直接执行函数
        private void None_void()
        {
            //再另建一个项目,在项目中引用上面生成的ClassLibrary1.DLL
            System.Type t = a.GetType("Class1.Class2");
            //动态生成ClassLibrary1.Class类的实例
            Object theObj = System.Activator.CreateInstance(t);
            //参数信息,GetSum需要两个string参数
            System.Type[] paramTypes = new System.Type[2];
            //此处调用方法,如果有参数只需要在括号的后面加上"," 加上参数的type[]类型的参数
            System.Reflection.MethodInfo mi = t.GetMethod("Mes");
            Object returnValue = mi.Invoke(theObj, null);

        }
        //没有返回值,传出参数接收返回值;
        private void Two()
        {
            Type t = a.GetType("Class1.Class1");
            Object theObj = Activator.CreateInstance(t);
            Type[] types=new Type[2];
            types[0]=Type.GetType("System.Int32");
            types[1]=Type.GetType("System.Int32");
            Object[] obj=new Object[2];
            obj[0]=2;
            obj[1]=3;
            MethodInfo mi = t.GetMethod("aa",types);
           Object returnValue=mi.Invoke(theObj,obj);
           this.textBox1.Text = returnValue.ToString();
        }
        //获取同一个命名空间的反射出值
        private void Local()
        {
            Type t=Type.GetType("fanshetest1.Class1");
            //创建一个 实例
            Object theObj = Activator.CreateInstance(t);
            Type[] types=new Type[2];
            types[0]=Type.GetType("System.Int32");
            types[1] = Type.GetType("System.Int32");
            Object[] obj = new Object[2];
            obj[0] = 2;
            obj[1] = 3;
            MethodInfo mi = t.GetMethod("aa",types);
            Object returnValue = mi.Invoke(theObj,obj);
            this.textBox1.Text = returnValue.ToString();
        }
        //获取出一个属性
        private void attribute()
        {
            a = Assembly.LoadFrom("Class1.dll");
            Type t = a.GetType("Class1.Class1");
            t = Type.GetType("fanshetest1.Class1");
            Object theObj = Activator.CreateInstance(t);
            t.GetProperty("AB").SetValue(theObj,"a",null);
            this.textBox1.Text = t.GetProperty("AB").GetValue(theObj, null).ToString();
        }
        //获取出静态的函数;
        private void static_()
        {
            a = System.Reflection.Assembly.LoadFrom("Class1.dll");
            Type t = a.GetType("Class1.Class1");
            t = Type.GetType("fanshetest1.Class1");
            //创建一个实例
            Object theObj = Activator.CreateInstance(t);
            //创建一个方法的实例
            MethodInfo mi = t.GetMethod("cc");
            Object returnValue = mi.Invoke(theObj, null);
            this.textBox1.Text = returnValue.ToString();
        }
        //获取出变量;
        private void variable()
        {
            a = Assembly.LoadFrom("Class1.dll");
            Type t = a.GetType("Class1.Class1");
            t = Type.GetType("fanshetest1.Class1");
            Object theObj = Activator.CreateInstance(t);
            MethodInfo mi = t.GetMethod("a");
            t.GetField("a").SetValue(theObj,"a");
            this.textBox1.Text = t.GetField("a").GetValue(theObj).ToString();
        }
        //获取出私有变量反射值;
        private void private_()
        {
           
        }
        //构造函数
        private void gouzaohanshu()
        {
            Type t = a.GetType("Class1.Class1");
            t = Type.GetType("fanshetest1.Class1");
            //创建构造函数类型
            Type[] structure_Type = new Type[1];
            structure_Type[0] = Type.GetType("System.String");
            //定义构造函数传参类型
            Object[] structure_Obj = new Object[1];
            structure_Obj[0] = "aa";
            //创建一个 实例
            Object theObj = Activator.CreateInstance(t,structure_Obj);
            //MethodInfo structure_Mi = t.GetConstructor(structure_Type);
            //MethodInfo structure_Mi = t.GetMethod("Class1", structure_Type);
            //structure_Mi.Invoke(theObj, structure_Obj);

            MethodInfo mi = t.GetMethod("a");
            //t.GetField("a").SetValue(theObj, "a");
            this.textBox1.Text = t.GetField("a").GetValue(theObj).ToString();
        }

    }
}

posted on 2009-02-04 16:52  这个Blog很强大  阅读(2214)  评论(0编辑  收藏  举报