熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

c# 反射的使用方法

2022-06-13   來源: .NET編程 

  在網上查找了不少的資料可以說大同小異概念性的東西網上一搜一堆今天把反射的東西整理了一下供大家使用我保證我這裡是最全面的東西當然也是基礎的東西在學好了這一切的基礎上大家可以學習反射的具體插件等應用老鳥就不用看了首先我們建立一個類庫將它生成為HelloWorlddll

  using System;

  namespace Webtest
     {

  public interface interface
         {
              int add();

  }
         public class ReflectTest:interface
         {

  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()
              {
                  thisWrite = Write;
                  thisWritec = Writec;
              }

  public ReflectTest(string strstring str)
             {
                 thisWrite = str;
                 thisWritec = str;

  }

  public string WriteString(string sint b)
             {
                 return 歡迎您 + s + + b; ;
             }

  public static string WriteName(string s)
              {
                 return 歡迎您光臨 + s;
              }

  public string WriteNoPara()
             {
                return 您使用的是無參數方法;
             }

  private string WritePrivate()
             {
                 return 私有類型的方法;
             }

  public int add()
             {
                 return ;
             }
         }
    }然後建立再建立一個項目引入該HelloWorlddll
    using System;

  using SystemThreading;
    using SystemReflection;

  class Test
    {
        delegate string TestDelegate(string valueint value);

  static void Main()
        {
            //Assembly t = AssemblyLoadFrom(HelloWorlddll); 與下面相同的效果
            Assembly t = AssemblyLoad(HelloWorld);

  foreach (Type aaa in tGetTypes())
           {
                //ConsoleWrite(aaaName);   //顯示該dll下所有的類
            }

  Module[] modules = tGetModules();

  foreach (Module module in modules)
            {
                //ConsoleWriteLine(module name: + moduleName);//顯示模塊的名字本例為HelloWorlddll
            }

  Type a = typeof(WebtestReflectTest);//得到具體的類的類型和下面一個效果

  //Type a = tGetType(WebtestReflectTest);//
            //ConsoleWrite(aName);

  string[] bb ={ aaaa bbbbb };
            object obj = ActivatorCreateInstance(abb); //創建該類的實例後面的bb為有參構造函數的參數
            //object obj = tCreateInstance(WebtestReflectTest);//與上面方法相同

  MethodInfo[] miArr = aGetMethods();
            foreach (MethodInfo mi in miArr)
           {
                //ConsoleWrite(miName);  //顯示所有的共有方法
           }

  MethodInfo mi = aGetMethod(WriteString);//顯示具體的方法
            object[] aa={使用的是帶有參數的非靜態方法};
            string s = (string)miInvoke(objaa); //帶參數方法的調用

  MethodInfo mi = aGetMethod(WriteName);
            String[] aa ={使用的是靜態方法};
            string s = (string)miInvoke(null aa); //靜態方法的調用

  MethodInfo mi = aGetMethod(WriteNoPara);
            string s = (string)miInvoke(obj null); //不帶參數的方法調用

  MethodInfo mi = aGetMethod(WritePrivateBindingFlagsInstance | BindingFlagsNonPublic);
            string s = (string)miInvoke(obj null); //私有類型方法調用

  //ConsoleWrite(s);

  PropertyInfo[] piArr = aGetProperties(BindingFlagsInstance | BindingFlagsNonPublic | BindingFlagsPublic);
            foreach (PropertyInfo pi in piArr)
            {
             //ConsoleWrite(piName);  //顯示所有的方法
            }

  PropertyInfo pi=aGetProperty(Writea);
            //piSetValue(obj Writea null);
            //ConsoleWrite(piGetValue(objnull));

  PropertyInfo pi = aGetProperty(Writeb BindingFlagsInstance | BindingFlagsNonPublic | BindingFlagsPublic);
            piSetValue(obj Writeb null);
            //ConsoleWrite(piGetValue(obj null));

  FieldInfo fi = aGetField(Write);
            //ConsoleWrite(fiGetValue(obj));

  ConstructorInfo[] ci = aGetConstructors();
            foreach (ConstructorInfo ci in ci)
            {
                //ConsoleWrite(ciToString()); //獲得構造函數的形式
            }

  ConstructorInfo asCI = aGetConstructor(new Type[] { typeof(string) typeof(string) });
            //ConsoleWrite(asCIToString());

  Webtestinterface obj = (Webtestinterface)tCreateInstance(WebtestReflectTest);
            WebtestReflectTest obj = (WebtestReflectTest)tCreateInstance(WebtestReflectTest);
            //ConsoleWrite(objadd());典型的工廠模式

  foreach (Type tt in tGetTypes())
            {
                if (ttGetInterface(interface)!=null)
                {
                    Webtestinterface obj = (Webtestinterface)ActivatorCreateInstance(a);
                    //ConsoleWrite(objadd());
                }
            }

  TestDelegate method = (TestDelegate)DelegateCreateDelegate(typeof(TestDelegate) obj WriteString);
             //動態創建委托的簡單例子
            ConsoleWrite(method(str ));
            ConsoleRead();
        }
    }
    在這裡我把我們常用的方法屬性等全部整理了出來大家不要嫌棄亂靜下心來自己按照我的分隔一部分一部分的來保證你對反射的學習會事半功倍當然有關於其方法我會繼續補充想了這麼些就先寫下來吧


From:http://tw.wingwit.com/Article/program/net/201311/12496.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.