A14_反射

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

namespace A14_Reflection
{
    class Person
    {
        public string Name;
        public int Age;
        private int _ID;
        public int ID
        {
            get{return _ID;}
            set{_ID = value;}
        }

        public void DispalyID()
        {
            Console.WriteLine("ID = "+_ID);
        }

        public void DispalyName()
        {
            Console.WriteLine("Name = " + Name);
        }

        public void DispalyAge()
        {
            Console.WriteLine("Age = " + Age);
        }

        //調用方法測試
        public void Test1()
        {
            Console.WriteLine("Person類Test1無參方法");
        }

        public void Test2(string str)
        {
            Console.WriteLine("Person類Test2無參方法,參數str = "+ str);
        }

        public void Test3()
        {
            Console.WriteLine("Person類Test3無參方法");
        }

        public void Test3(int num)
        {
            Console.WriteLine("Person類Test2無參方法,參數str = " + num);
        }

        private void DisplayPrivateMethod()
        {
            Console.WriteLine("Person類私有都方法調用");
        }
    }
}


/*
 * 1.反射是通過程序,瞭解及調用“程序集”的相關屬性,方法,字段
 * 通過反射可讀取程序集(*.exe  *.dll)中代碼的內容
 * 可以根據(字符串)類名,來動態創建類的對象
 * 可以動態獲取對象中的方法、屬性、字段
 * 可以根據(字符串)方法名稱來調用執行。
 * 
 * 使用反射需要引入命名空間 System.Reflaction
 * Type類實例可以獲取類的名稱、所屬命名空間、是否爲公共類以及字段、屬性、方法等信息
 * 
 * 
 * **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Reflection;

namespace A14_Reflection
{
    class Demo1
    {
        public void Test1()
        {
            Person per = new Person();
            Type tyPersonObj = per.GetType();
            //顯示類的名稱
            Console.WriteLine(tyPersonObj.Name);
            //顯示類所屬的命名空間
            Console.WriteLine(tyPersonObj.Namespace);
            //顯示類所屬的程序集
            Console.WriteLine(tyPersonObj.Assembly);
            //獲取類是否爲公共,密封,私有
            Console.WriteLine(tyPersonObj.IsPublic);
            Console.WriteLine(tyPersonObj.IsSealed);
            Console.WriteLine(tyPersonObj.IsPrimitive);
        
        }

        public void Test2()
        {
            Person per = new Person();
            Type tyPersonObj = per.GetType();
            Console.WriteLine("-----------------------------------------------");
            //得到字段信息
            FieldInfo[] fieArray = tyPersonObj.GetFields();
            foreach (FieldInfo item in fieArray)
            {
                Console.WriteLine(item.Name);
            }
            Console.WriteLine("-----------------------------------------------");
            //得到方法信息
            MethodInfo[] metArray = tyPersonObj.GetMethods();
            foreach (MethodInfo item in metArray)
            {
                Console.WriteLine(item.Name);
            }
            Console.WriteLine("-----------------------------------------------");
            //得到屬性信息
            PropertyInfo[] proArray = tyPersonObj.GetProperties();
            foreach (PropertyInfo item in proArray)
            {
                Console.WriteLine(item.Name);
            }
        }


        static void Main1(string[] args)
        {
            Demo1 obj = new Demo1();
            obj.Test1();
            obj.Test2();
        }
    }
}

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

using System.Reflection;

namespace A14_Reflection
{
    class Dome2
    {
        //通過程序集中的一個類的實例化,從而得到這個“程序集”(Assembly)的對象
        public void Test1()
        {
            Person per = new Person();
            //得到程序集
            Assembly assObj = per.GetType().Assembly;
            Console.WriteLine(assObj.FullName);
            Console.WriteLine("---------------------------------------------");
            Type[] typArray = assObj.GetTypes();
            foreach (Type item in typArray)
            {
                Console.WriteLine(item.Name);
            }
        }
        

        //使用路徑的方式直接得到程序集
        public void Test2()
        {
            Console.WriteLine("直接訪問程序集的測試");
            //通過路徑的方式,直接的道程序集的對象
            Assembly assObj = Assembly.LoadFrom(@"G:\System.dll");
            Type[] typArray = assObj.GetTypes();
            foreach (Type item in typArray)
            {
                Console.WriteLine(item.Name);
            }
        }
        
        static void Main1(string[] args)
        {
            Dome2 obj = new Dome2();
            obj.Test1();
            obj.Test2();
        }

    }
}


/*
 * 使用反射技術動態調用
 * 缺點:運行速度慢
 * 優點:可以使得類對象之間的調用關係,在運行其進行低偶合調用,
 *       可以隨着配置文件的改變,進行改變。
 * 
 * ***/

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

using System.Reflection;

namespace A14_Reflection
{

    class Demo3
    {
        //通過反射調用方法和屬性
        public void Test1()
        {
            string strClassName = "Person";
            string strMethodName = "Test1"; 
            //的道程序集對象
            Assembly assObj = Assembly.LoadFrom(@"G:\A14_Reflection.exe");
            //得到type
            Type typObj = assObj.GetType("A14_Reflection." + strClassName);
            //創建對象
            object obj = Activator.CreateInstance(typObj);
            //得到方法
            MethodInfo methInfo = typObj.GetMethod(strMethodName);
            //調用無參方法
            methInfo.Invoke(obj,null);
            
        }

        //調用有參方法
        public void Test2()
        {
            string strClassName = "Person";
            string strMethodName = "Test2";
            //的道程序集對象
            Assembly assObj = Assembly.LoadFrom(@"G:\A14_Reflection.exe");
            //得到type
            Type typObj = assObj.GetType("A14_Reflection." + strClassName);
            //創建對象
            object obj = Activator.CreateInstance(typObj);
            //得到方法
            MethodInfo methInfo = typObj.GetMethod(strMethodName);
            //調用有參方法
            object[] objArray = new object[1];
            objArray[0] = "張三";
            methInfo.Invoke(obj,objArray);
        }

        //調用重載方法
        public void Test3()
        {
            string strClassName = "Person";
            string strMethodName = "Test3";
            //的道程序集對象
            Assembly assObj = Assembly.LoadFrom(@"G:\A14_Reflection.exe");
            //得到type
            Type typObj = assObj.GetType("A14_Reflection." + strClassName);
            //創建對象
            object obj = Activator.CreateInstance(typObj);
            //得到無參重載方法
            MethodInfo methInfo = typObj.GetMethod(strMethodName,new Type[0]);
            //調用無參重載方法
            methInfo.Invoke(obj, null);

            //得到有參重載方法
            Type[] typArrayWithPara = { typeof(int) }; //確定方法重載中有參數
            MethodInfo methInfo2 = typObj.GetMethod(strMethodName, typArrayWithPara);
            //創建參數列表
            object[] objArray = new object[1];
            objArray[0] = 15;
            //調用無參重載方法
            methInfo2.Invoke(obj, objArray);
        }

        //演示私有方法
        public void Test4()
        { 
            
            string strClassName = "Person";
            string strMethodName = "DisplayPrivateMethod"; 
            //的道程序集對象
            Assembly assObj = Assembly.LoadFrom(@"G:\A14_Reflection.exe");
            //得到type
            Type typObj = assObj.GetType("A14_Reflection." + strClassName);
            //創建對象
            object obj = Activator.CreateInstance(typObj);
            //得到方法
            MethodInfo methInfo = typObj.GetMethod(strMethodName, BindingFlags.NonPublic | BindingFlags.Instance);
            //調用無參方法
            methInfo.Invoke(obj,null);
            
        }

        //演示屬性的調用
        public void Test5()
        {
            string strClassName = "Person";
            string stdrPropertyName = "ID"; //屬性的名稱
            //的道程序集對象
            Assembly assObj = Assembly.LoadFrom(@"G:\A14_Reflection.exe");
            //得到type
            Type typObj = assObj.GetType("A14_Reflection." + strClassName);
            //創建對象
            object obj = Activator.CreateInstance(typObj);
            //得到屬性
            PropertyInfo propInfo = typObj.GetProperty(stdrPropertyName);
            //設置屬性
            propInfo.SetValue(obj, 1000);
            //讀取屬性的值
            string strResult = propInfo.GetValue(obj, null).ToString();
            Console.WriteLine(strResult);
        }


        static void Main1(string[] args)
        {
            Demo3 obj = new Demo3();
            obj.Test1();
            obj.Test2();
            obj.Test3();
            obj.Test4();
            obj.Test5();
        }
    }
}

/*測試父類**/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A14_Reflection
{
    public class Man
    {
    }
}


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

namespace A14_Reflection
{
    public class ZhangSan:Man,ICanSpeak
    {
    }
}


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

namespace A14_Reflection
{
    public class Dog
    {
    }
}


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

namespace A14_Reflection
{
    interface ICanSpeak
    {
    }
}


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

using System.Reflection;
namespace A14_Reflection
{
    class Demo4
    {
        //判斷參數類是否爲一個指定類的父類或接口
        public void Test1()
        {
            Type typeMen = typeof(Man);
            Type typeZahngSan = typeof(ZhangSan);
            Type typeDog = typeof(Dog);
            Type typeICanSpeak = typeof(ICanSpeak);
            //判斷父類
            Console.WriteLine("類型Man是否爲類型ZhangSan的父類:"+typeMen.IsAssignableFrom(typeZahngSan));
            Console.WriteLine("類型Dog是否爲類型ZhangSan的父類:" + typeDog.IsAssignableFrom(typeZahngSan));
            //判斷接口
            Console.WriteLine("爲類型ZhangSan是否實現了接口ICanSpeak:" + typeICanSpeak.IsAssignableFrom(typeZahngSan));
            Console.WriteLine("爲類型Dog是否實現了接口ICanSpeak:" + typeICanSpeak.IsAssignableFrom(typeDog));
        }

        public void Test2()
        {
            Type typeMen = typeof(Man);
            Type typeZahngSan = typeof(ZhangSan);
            ZhangSan zhangsanObj = new ZhangSan();
            Type typeICanSpeak = typeof(ICanSpeak);
            Dog dogObj = new Dog();
            Man manObj = new Man();

            //判斷是否爲實例
            Console.WriteLine("menObj是否是類型Man的實例:" + typeMen.IsInstanceOfType(manObj));
            Console.WriteLine("zhangsanObj是否是類型Man的實例:" + typeMen.IsInstanceOfType(zhangsanObj));
            Console.WriteLine("dogObj是否是類型Man的實例:" + typeMen.IsInstanceOfType(dogObj));

            //判斷實例是否實現了接口
            Console.WriteLine("實例zhangsanObj是否實現了接口ICanSpeak:" + typeICanSpeak.IsInstanceOfType(zhangsanObj));
            Console.WriteLine("實例dogObj是否實現了接口ICanSpeak:" + typeICanSpeak.IsInstanceOfType(dogObj));
            Console.WriteLine("實例manObj是否實現了接口ICanSpeak:" + typeICanSpeak.IsInstanceOfType(manObj));
        }

        //判斷一個(參數)類是否爲一個類的子類
        public void Test3()
        {
            Type typeMen = typeof(Man);
            Type typeZahngSan = typeof(ZhangSan);
            Type typeDog = typeof(Dog);

            //判斷是否爲實例
            Console.WriteLine("類型ZhangSan是否爲類型Man的子類:" + typeZahngSan.IsSubclassOf(typeMen));
            Console.WriteLine("類型ZhangSan是否爲類型Man的子類:" + typeDog.IsSubclassOf(typeMen));
        }

        public void Test4()
        {

        }

        static void Main(string[] args)
        {
            Demo4 obj = new Demo4();
            //obj.Test1();
            //obj.Test2();
            obj.Test3();
        }
    }
}







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章