C#類的反射執行個體

來源:互聯網
上載者:User
建立在一個命名空間下的類 Class1和不在同一個命名空間下的 Class1
(在這裡這些類只使用於被調用的,意義不大。反射用法在Form類裡面)

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

    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.