c# -反射 初見

反射是一個很強大的功能,不過好像有些消耗性能,你們慎重使用。數組

1.反射是幹什麼的?ide

經過反射,咱們可與獲取程序集中的原數據。函數

1.什麼是程序集?性能

dll、exe  這些將不少能實現具體功能的代碼封裝起來的文件(我本身的理解,可能不對!)。this

2.用到的狀況有哪些?spa

編譯器的提示功能、反編譯、還有調用別人的dll時,其它我不知道的。。code

3.下面直接奉上一個實例的代碼,供參考。對象

(1)先建一個叫作Common的類庫,在裏面建一個叫Person的類,類的代碼以下。blog

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

namespace Common
{
    public class Person
    {
        //姓名
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        //年齡
        private int _age;
        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }
 
        //打印姓名和年齡
        public void printfNameAge()
        {
            Console.WriteLine(_name);
            Console.WriteLine(_age);
        }

        //有參數的構造函數
        public Person(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }

    }
}
View Code

(2)在建一個窗體程序,其中的Program.cs代買以下,裏面實現了一些反射的經常使用方法。get

在運行這個程序以前,你要將Common編譯一下,而後去Debug中將Common.dll拷貝到你建好程序的Debug中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;//反射
//Author  崔時雨
//Date   20160808
namespace 反射
{
    class Program
    {
        static void Main(string[] args)
        {
            //1.加載程序集文件
            string path = AppDomain.CurrentDomain.BaseDirectory + "Common.dll";
            Assembly ass = Assembly.LoadFile(path);

            //2. 獲取數據集數據的三個函數
            //2.1 獲取指定對象的類型
            Type t = ass.GetType("Common.Person");
            Console.WriteLine(t.Name);

            //2.2 獲取元數據,不管公有私有。
            Type[] type1 = ass.GetTypes();
            Console.WriteLine("打印2.2 獲取元數據,不管公有私有。");
            foreach (Type item in type1)
            {
                Console.WriteLine(item.Name);
                Console.WriteLine(item.FullName);
                Console.WriteLine(item.Namespace);
            }

            //2.3 值獲取公有的
            Type[] type2 = ass.GetExportedTypes();
            Console.WriteLine("\n打印2.3 值獲取公有的");
            foreach (Type item in type2)
            {
                Console.WriteLine(item.Name);
                Console.WriteLine(item.FullName);
                Console.WriteLine(item.Namespace);
            }

            //3 建立對象
            //3.1調用person中的默認無參的構造函數
            // object obj1 = ass.CreateInstance("Common.Person");//一般不用這種,若是存在有參數的構造函數,不能建立。
            //3.2 能夠構造函數有參數的對象
            object obj2 = Activator.CreateInstance(t/*對象類型*/, "小明", 18);
            //3.2.1得到數據源的屬性數組 
            PropertyInfo[] strPro = obj2.GetType().GetProperties();
            Console.WriteLine("\n打印3.2.1得到數據源的屬性數組");
            foreach (PropertyInfo item in strPro)
            {
                Console.WriteLine(item.Name);
            }
            //3.2.2得到數據源的方法數組 
            MethodInfo[] methods = obj2.GetType().GetMethods();
            Console.WriteLine("\n打印3.2.2得到數據源的方法數組 ");
            foreach (MethodInfo item in methods)
            {
                Console.WriteLine(item.Name);
            }

            //3.3調用函數,打印。
            MethodInfo md = obj2.GetType().GetMethod("printfNameAge");
            Console.WriteLine("\n調用 Person中的printfNameAge方法,打印 姓名和年齡");
            md.Invoke(obj2,null);

            Console.ReadKey();
        }
    }
}
View Code

4.運行結果

 

總結:

參考了不少人的博客,發現要想弄清楚反射還須要許多其它的知識,使用這篇內容的知識,已經能夠簡單的使用反射了。

知識點總要一個一個學,積累的多了,就會連成串。

相關文章
相關標籤/搜索