C#反射執行個體(一) 利用反射使用類庫

來源:互聯網
上載者:User

在網上尋找了不少的資料,可以說大同小異,概念性的東西網上一搜一堆,今天把反射的東西整理了一下,供大家使用,我保證我這裡是最全面的東西,當然也是基礎的東西,在學好了這一切的基礎上,大家可以學習反射的具體外掛程式等應用,老鳥就不用看了。

//首先我們建立一個類庫,將它產生為HelloWorld.dll,

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

namespace Webtest
{
    using System;

    namespace Webtest
    {

        public interface interface1
        {
            int add();

        }
        public class ReflectTest : interface1
        {

            public String Write;
            private String Writec;

            public String Writea
            {
                get
                {
                    return Write;
                }
                set
                {
                    Write = value;
                }

            }

            private String Writeb
            {
                get
                {
                    return Writec;
                }
                set
                {
                    Writec = value;
                }

            }

            public ReflectTest()
            {
                this.Write = "Write";
                this.Writec = "Writec";
            }

            public ReflectTest(string str1, string str2)
            {
                this.Write = str1;
                this.Writec = str2;

            }

            public string WriteString(string s, int b)
            {
                return "\n這是類庫函數列印的字串:歡迎您," + s + "---" + b; ;
            }

            public static string WriteName(string s)
            {
                return "\n這是類庫函數列印的字串:歡迎您光臨," + s;
            }

            public string WriteNoPara()
            {
                return "\n這是類庫函數列印的字串:您使用的是無參數方法";
            }

            private string WritePrivate()
            {
                return "\n這是類庫函數列印的字串:私人類型的方法";
            }

            public int add()
            {
                return 5;
            }
        }
    }

}

//然後,建立再建立一個項目引入該HelloWorld.dll,

using System;
using System.Threading;
using System.Reflection;
using Webtest.Webtest;

class Test
{
    delegate string TestDelegate(string value, int value1);

    static void Main()
    {
        //Assembly t = Assembly.LoadFrom("Webtest.dll"); //與下面相同的效果
        Assembly t = Assembly.Load("Webtest");

        Console.Write("Webtest類庫裡有以下類:\n");
        foreach (Type aaa in t.GetTypes())
        {
            Console.Write(aaa.Name + "\n");   //顯示該dll下所有的類
        }

        Module[] modules = t.GetModules();
        Console.WriteLine();
        foreach (Module module in modules)
        {
            Console.WriteLine("模組名稱:\n" + module.Name);//顯示模組的名字本例為"HelloWorld.dll"
        }

        Type a = typeof(Webtest.Webtest.ReflectTest);//得到具體的類的類型,和下面一個效果
        //Type a = t.GetType("Webtest.ReflectTest");
        //Console.Write("模組裡的類有:" + a.Name + "\n");

        string[] bb ={ "aaaaa", "bbbbb" };
        object obj = Activator.CreateInstance(a, bb); //建立該類的執行個體,後面的bb為有參建構函式的參數
        //object obj = t.CreateInstance("Webtest.ReflectTest");//與上面方法相同

        //獲得執行個體公用方法的集合
        MethodInfo[] miArr = a.GetMethods();
        Console.Write("\n共有方法: \n");
        foreach (MethodInfo mi0 in miArr)
        {
            Console.Write(mi0.Name + "\n"); //顯示所有的共有方法
        }

        MethodInfo mi = a.GetMethod("WriteString");//顯示具體的方法
        object[] aa ={ "使用的是帶有參數的非靜態方法", 2 };
        string s = (string)mi.Invoke(obj, aa); //帶參數方法的調用

        MethodInfo mi1 = a.GetMethod("WriteName");
        String[] aa1 ={ "使用的是靜態方法" };
        string s1 = (string)mi1.Invoke(null, aa1); //靜態方法的調用

        MethodInfo mi2 = a.GetMethod("WriteNoPara");
        string s2 = (string)mi2.Invoke(obj, null); //不帶參數的方法調用

        MethodInfo mi3 = a.GetMethod("WritePrivate", BindingFlags.Instance | BindingFlags.NonPublic);
        string s3 = (string)mi3.Invoke(obj, null); //私人類型方法調用

        //Console.Write(s3);

        //獲得執行個體公用屬性的集合
        PropertyInfo[] piArr = a.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
        Console.WriteLine("\n顯示所有的屬性:");
        foreach (PropertyInfo pi in piArr)
        {
            Console.WriteLine(pi.Name); //顯示所有的屬性
        }

        PropertyInfo pi1 = a.GetProperty("Writea");
        //pi1.SetValue(obj, "Writea", null);
        //Console.Write(pi1.GetValue(obj,null));

        PropertyInfo pi2 = a.GetProperty("Writeb", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
        pi2.SetValue(obj, "Writeb", null);
        //Console.Write(pi2.GetValue(obj, null));

        FieldInfo fi1 = a.GetField("Write");
        //Console.Write(fi1.GetValue(obj));

        //獲得執行個體公用建構函式的集合
        ConstructorInfo[] ci1 = a.GetConstructors();
        Console.WriteLine("\n顯示所有的建構函式:");
        foreach (ConstructorInfo ci in ci1)
        {
            Console.Write(ci.ToString()+"\n"); //獲得建構函式的形式
        }
        //ConstructorInfo asCI = a.GetConstructor(new Type[] { typeof(string), typeof(string) });
        //Console.Write(asCI.ToString());

        Webtest.Webtest.interface1 obj1 = (Webtest.Webtest.interface1)t.CreateInstance("Webtest.ReflectTest");
        Webtest.Webtest.ReflectTest obj2 = (Webtest.Webtest.ReflectTest)t.CreateInstance("Webtest.ReflectTest");
        //Console.Write(obj1.add());典型的原廠模式

        foreach (Type tt in t.GetTypes())
        {
            if (tt.GetInterface("interface1") != null)
            {
                Webtest.Webtest.interface1 obj3 = (Webtest.Webtest.interface1)Activator.CreateInstance(a);
                //Console.Write(obj3.add());
            }
        }

        TestDelegate method = (TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate), obj, "WriteString");
        //動態建立委託的簡單例子
        Console.Write(method("str1", 2));
        Console.Read();
    }
}

    在這裡我把我們常用的方法,屬性,等全部整理了出來,大家不要嫌棄亂,靜下心來,自己按照我的分隔一部分一部分的來,保證你對反射的學習,會事半功倍.當然有關於其方法我會繼續補充,想了這麼些就先寫下來吧.

不是強者,將來會是。

 

http://www.cnblogs.com/leonn/archive/2009/03/11/1408303.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.