欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#反射之基礎(chǔ)應(yīng)用實(shí)例總結(jié)

 更新時(shí)間:2014年10月10日 09:28:58   投稿:shichen2014  
這篇文章主要介紹了C#反射之基礎(chǔ)應(yīng)用實(shí)例總結(jié),包括了反射的基本原理與用法實(shí)例,需要的朋友可以參考下

本文將反射的東西整理了一下 , 提供了最全面的東西 , 當(dāng)然也是基礎(chǔ)的東西 ,
在學(xué)好了這一切的基礎(chǔ)上 , 大家可以學(xué)習(xí)反射的具體插件等應(yīng)用 首先我們建立一個(gè)類庫 , 將它生成為 reflectPrj .dll,
 

復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
 
namespace reflectPrj
{
    /// <summary>
    /// 接口
    /// </summary>
    public interface Interface1
    {
        int Add(int num);
        int Add();
    }
    /// <summary>
    /// 用來被測(cè)試的類
    /// </summary>
    public class ReflectTest : Interface1
    {
  
        public string writea;
 
        public string WriteA
        {
            get { return writea; }
            set { writea = value ; }
        }
 
        public string Writeb;
 
        public string WriteB
        {
            get { return Writeb; }
            set { Writeb = value ; }
        }
 
        public ReflectTest()
        {
            this .WriteA = "WriteA" ;
            this .WriteB = "WriteB" ;
        }
 
        public ReflectTest(string a, string b)
        {
            this .WriteA = a;
            this .WriteB = b;
        }
 
        public int Add()
        {
            return 100;
        }
        public int Add(int num)
        {
            return num;
        }
 
        public string WriteString(string a,string b)
        {
             return " 歡迎你," + a + "------" +b;
      
        }
 
        public static string WriteName(string s)
        {
            return " 歡迎光臨," + s;
        }
 
        public string WriteNoPara()
        {
            return " 你使用的是無參數(shù)方法!" ;
        }
 
        private string WritePrivate()
        {
            return " 私有類型的方法!" ;
        }
    }
}

 
之后再建立一個(gè)項(xiàng)目引入該 reflectPrj .dll
 
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using reflectPrj;
using System.Threading;
using System.Reflection;
 
namespace reflectPrjTest
{
    class MyReflectTest
    {
        // 建立委托
        delegate string TestDelegate (string a,string b);
 
        static void Main(string [] args)
        {
            Assembly assembly= Assembly .Load("reflectPrj" );
 
            foreach (Type var in assembly.GetTypes())
            {
                Console .WriteLine(var.Name);// 顯示dll 下所有的類
            }
 
            //*******************************************************
 
            Module [] modules = assembly.GetModules();
 
            foreach (Module module in modules)
            {
                Console .WriteLine("module( 模塊/ 組件) 名:" +module.Name);
            }
 
            //*******************************************************
            // 得到具體的類的類型
            Type a = typeof (reflectPrj.ReflectTest );
          
            Console .WriteLine(a.Name);
 
            //*******************************************************
            //A------ 創(chuàng)建類型的實(shí)例----> 此處是由帶參的構(gòu)造函數(shù)的來得到的實(shí)例
            string [] paras ={"aaa" ,"bbb" };
            // 創(chuàng)建該類的實(shí)例,后面的paras 為有參構(gòu)造函數(shù)的參數(shù)----> 此obj 即是類型a 的一個(gè)實(shí)例
            // 此實(shí)例調(diào)用的是一個(gè)帶參的構(gòu)造函數(shù)
            object obj = Activator .CreateInstance(a,paras);
 
            // 得到對(duì)象的屬性
            Console .WriteLine(" 得到對(duì)象a 的屬性:" );
            foreach (object var in a.GetProperties())
             {
                 Console .WriteLine(var.ToString());
             }
          
            MethodInfo [] miArr = a.GetMethods();
 
            Console .WriteLine(" 顯示所有的共有方法:" );
            // 顯示所有的共有方法
            foreach (MethodInfo method in miArr)
            {
                Console .WriteLine(method.Name);
            }
          
            //************************************************************
            // 顯示具體的方法
            Console .WriteLine(" 顯示具體的方法!" );
 
            //1. 帶參的方法的使用
            MethodInfo mi = a.GetMethod("WriteString" );
            string miReturn =(string ) mi.Invoke(obj, new object [] { " 使用的是帶參數(shù)的非靜態(tài)方法" , "2" });
 
            Console .WriteLine("---" +mi.Name+" 返回值:" +miReturn);
 
            //2. 不帶參數(shù)的方法調(diào)用
            Console .WriteLine(" 不帶參數(shù)的方法的調(diào)用:" );
            MethodInfo miNopara = a.GetMethod("WriteNoPara" );
 
            string miNoparaReturn = (string )miNopara.Invoke(obj, null );
 
            Console .WriteLine("---" +miNoparaReturn);
 
            //3. 私有類型方法的使用
            Console .WriteLine(" 私有類型方法的使用:" );
            MethodInfo miPrivate = a.GetMethod("WritePrivate" ,BindingFlags .Instance | BindingFlags .NonPublic);
 
            string miPrivateReturn = (string )miPrivate.Invoke(obj, null );
 
            Console .WriteLine("---" +miPrivateReturn);
 
            Console .WriteLine("*********************** 屬性的使用**********************" );
 
            //4. 得到對(duì)象的屬性
            PropertyInfo [] propertys = a.GetProperties(BindingFlags .Instance | BindingFlags .NonPublic |BindingFlags .Public);
         
            //5. 顯示所有屬性名
            Console .WriteLine(" 對(duì)象的所有屬性名如下:" );
            foreach (PropertyInfo pro in propertys)
            {
                //Console.WriteLine(pro.Name);
                 // 獲取屬性最初的值
                Console .WriteLine(pro.Name+" :" +pro.GetValue(obj,null ));
 
                // 給屬性重新賦值
                pro.SetValue(obj, " 張三豐" , null );
                Console .WriteLine(pro.Name + " :" + pro.GetValue(obj, null ));
 
             }
        
            //6. 獲取指定的屬性,并賦值
            PropertyInfo propertyInfo=a.GetProperty("WriteA" ,BindingFlags .Instance| BindingFlags .NonPublic|BindingFlags .Public);
 
            propertyInfo.SetValue(obj, " 郁金香" , null );
 
            Console .WriteLine(propertyInfo.Name+" :" +propertyInfo.GetValue(obj,null ));
 
            Console .WriteLine("*********************FieldInfo--- 公開字段的使用***********************" );
            //7. 字段的使用----> 只能獲取公開字段
            FieldInfo f1 = a.GetField("writea" ,BindingFlags .Instance| BindingFlags .NonPublic| BindingFlags.Public);
 
            Console .WriteLine(f1.GetValue(obj));
 
            try
            {
                reflectPrj.ReflectTest test = new ReflectTest ("Marry" , "Jack" );
 
                Type myReflect = typeof (ReflectTest );
 
                FieldInfo field1= myReflect.GetField("writea" , BindingFlags .Public | BindingFlags .NonPublic |BindingFlags .Instance);
 
                Console .WriteLine(field1.GetValue(test));
            }
            catch (Exception ex)
             {
                Console .WriteLine(ex.Message);
            }
 
            //*******************************************************
            //8. 構(gòu)造函數(shù)的使用
            Console .WriteLine(" 獲得構(gòu)造函數(shù)的形式" );
            ConstructorInfo [] cis =a.GetConstructors();
            foreach (ConstructorInfo ci in cis)
            {
                Console .WriteLine(" 構(gòu)造函數(shù)的形式:" +ci.ToString());// 獲得構(gòu)造函數(shù)的形式
                Console .WriteLine(" 構(gòu)造函數(shù)名稱:" +ci.Name);
            }
 
            // 打印帶參構(gòu)造函數(shù)的形式
            ConstructorInfo ascSingle = a.GetConstructor(new Type [] { typeof (string ),typeof (string )});
 
            Console .WriteLine(ascSingle.ToString());
 
            //****************************************************
            //9. 工廠模式
            Console .WriteLine(a.Name);
            reflectPrj.Interface1 reflectObj2 = (Interface1 )assembly.CreateInstance("reflectPrj.ReflectTest" );
            reflectPrj.ReflectTest reflectObj1 = (ReflectTest )assembly.CreateInstance("reflectPrj.ReflectTest" );
 
            Console .WriteLine(reflectObj2.Add());// 典型的工廠模式,在后續(xù)真正應(yīng)用中使用的是接口中的方法(而接口又由實(shí)現(xiàn)接口的類來實(shí)現(xiàn))
            Console .WriteLine(reflectObj1.WriteNoPara());
 
            //10 工廠模式--- 方法的重載
            int num = 300;
            Console .WriteLine(reflectObj1.Add(300));
 
            Console .WriteLine(" 工廠模式的再一次實(shí)現(xiàn)!" );
            foreach (Type type in assembly.GetTypes())
            {
                if (type.GetInterface("reflectPrj.Interface1" )!=null )
                {
                    // 由接口的實(shí)現(xiàn)類來實(shí)現(xiàn)該接口
                    reflectPrj.Interface1 interfaceObj3 = (Interface1 )Activator .CreateInstance(a);
 
                    Console .WriteLine(interfaceObj3.Add());
                    Console .WriteLine(interfaceObj3.Add(600));
                }
            }
            //****************************************************
            //11. 動(dòng)態(tài)創(chuàng)建委托的簡(jiǎn)單例子---> 將委托綁定到目標(biāo)方法上
            TestDelegate myMothod = (TestDelegate )Delegate .CreateDelegate(typeof (TestDelegate ),obj,mi);
 
            Console .WriteLine(myMothod.Invoke("Tom" , "Jack" ));
 
            //****************************************************
            //B----------. 無參構(gòu)造函數(shù)的使用
            Console .WriteLine(" 無參構(gòu)造函數(shù)的使用!" );
 
            //objNonePara 是由ReflectTest 類的構(gòu)造函數(shù)所產(chǎn)生的對(duì)象
            object objNonePara = Activator .CreateInstance(a);
 
            string info=((reflectPrj.ReflectTest )objNonePara).WriteNoPara();
 
            Console .WriteLine(info);
 
            PropertyInfo proWriteA = a.GetProperty(((ReflectTest )objNonePara).WriteA);
            Console .WriteLine(proWriteA.GetValue(objNonePara,null ));
 
            proWriteA.SetValue(objNonePara, " 小小郁金香" , null );
 
            Console .WriteLine(proWriteA.GetValue(objNonePara, null ));
 
            //C:--------- 再次用帶參的構(gòu)造函數(shù)來產(chǎn)生實(shí)例
            ReflectTest objPara =(ReflectTest ) Activator .CreateInstance(a, new object [] {" 小郁" ," 來吧" });
            Console .WriteLine(objPara.WriteA+"/t" +objPara.WriteB);
            Console .WriteLine(objPara.WriteNoPara());
 
            // 調(diào)用帶參的方法
            Console .WriteLine(objPara.WriteString(" 大郁" , " 回家吧!" ));
            MethodInfo myMi=a.GetMethod("WriteString" );
 
             // 利用委托動(dòng)態(tài)的將將委托綁定的指定的方法
            myMothod=(TestDelegate )Delegate .CreateDelegate(typeof (TestDelegate ),objPara,myMi);
            Console .WriteLine(myMothod.Invoke(" 過年啦" , " 牛年快樂??!" ));
 
            // 屏幕暫停
            Console .ReadLine();
        }
     }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • C# 復(fù)制與刪除文件的實(shí)現(xiàn)方法

    C# 復(fù)制與刪除文件的實(shí)現(xiàn)方法

    這篇文章主要介紹了C# 復(fù)制與刪除文件的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • Unity命令行打包WebGL的示例代碼

    Unity命令行打包WebGL的示例代碼

    這篇文章主要介紹了Unity命令行打包WebGL的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • c# 基于任務(wù)的異步編程模式(TAP)的異常處理

    c# 基于任務(wù)的異步編程模式(TAP)的異常處理

    這篇文章主要介紹了c# 基于任務(wù)的異步編程模式的異常處理方法,幫助大家更好的理解和學(xué)習(xí)c#
    2020-11-11
  • C#實(shí)現(xiàn)快遞api接口調(diào)用方法

    C#實(shí)現(xiàn)快遞api接口調(diào)用方法

    這篇文章主要介紹了C#實(shí)現(xiàn)快遞api接口調(diào)用方法,主要是通過快遞API網(wǎng)接口的服務(wù),使用的時(shí)候直接申請(qǐng)個(gè)接口UID即可,有需要的小伙伴來參考下吧。
    2015-03-03
  • C#制作二維柱狀圖方法

    C#制作二維柱狀圖方法

    在本文里小編為各位分享的是關(guān)于C#制作二維柱狀圖方法和步驟,需要的讀者們學(xué)習(xí)下。
    2018-12-12
  • c# 實(shí)現(xiàn)雪花分形的示例

    c# 實(shí)現(xiàn)雪花分形的示例

    這篇文章主要介紹了c# 實(shí)現(xiàn)雪花分形的示例,幫助大家更好的利用c#繪制圖像,感興趣的朋友可以了解下
    2020-10-10
  • C# File類中的文件讀寫方法詳解

    C# File類中的文件讀寫方法詳解

    C#提供了多種操作文件的方案,尤其是File類中封裝的靜態(tài)方法,本文將通過一些簡(jiǎn)單的示例為大家講講C#讀寫文件的方法,需要的可以參考一下
    2023-05-05
  • C#中類與結(jié)構(gòu)的區(qū)別實(shí)例分析

    C#中類與結(jié)構(gòu)的區(qū)別實(shí)例分析

    這篇文章主要介紹了C#中類與結(jié)構(gòu)的區(qū)別,類與結(jié)構(gòu)是C#初學(xué)者比較輕易混淆的概念,本文加以實(shí)例說明,需要的朋友可以參考下
    2014-08-08
  • c# 異步編程基礎(chǔ)講解

    c# 異步編程基礎(chǔ)講解

    這篇文章主要介紹了c# 異步編程的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • C#利用GDI+繪制旋轉(zhuǎn)文字等效果實(shí)例

    C#利用GDI+繪制旋轉(zhuǎn)文字等效果實(shí)例

    這篇文章主要介紹了C#利用GDI+繪制旋轉(zhuǎn)文字等效果實(shí)例,是非常實(shí)用的重要技巧,需要的朋友可以參考下
    2014-09-09

最新評(píng)論