您现在的位置是:首页 >

java反射机制实现原理 c# 反射的使用方法

火烧 2022-01-09 21:22:24 1053
c# 反射的使用方法   在网上查找了不少的资料 可以说大同小异 概念性的东西网上一搜一堆 今天把反射的东西整理了一下 供大家使用 我保证我这里是最全面的东西 当然也是基础的东西 在学好了这一切的基础

c# 反射的使用方法  

  在网上查找了不少的资料 可以说大同小异 概念性的东西网上一搜一堆 今天把反射的东西整理了一下 供大家使用 我保证我这里是最全面的东西 当然也是基础的东西 在学好了这一切的基础上 大家可以学习反射的具体插件等应用 老鸟就不用看了 首先我们建立一个类库 将它生成为HelloWorld dll

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

  public ReflectTest(string str string str )             {                 this Write = str ;                 this Writec = str ;

  }

  public string WriteString(string s int b)             {                 return 欢迎您 + s + + b; ;             }

  public static string WriteName(string s)              {                 return 欢迎您光临 + s;              }

  public string WriteNoPara()             {                return 您使用的是无参数方法 ;             }

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

  public int add()             {                 return ;             }         }    }然后 建立再建立一个项目引入该HelloWorld dll     using System;

  using System Threading;    using System Reflection;

java反射机制实现原理 c# 反射的使用方法

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

  static void Main()        {            //Assembly t = Assembly LoadFrom( HelloWorld dll ); 与下面相同的效果            Assembly t = Assembly Load( HelloWorld );

  foreach (Type aaa in t GetTypes())           {                //Console Write(aaa Name);   //显示该dll下所有的类            }

  Module[] modules = t GetModules();

  foreach (Module module in modules)            {                //Console WriteLine( module name: + module Name);//显示模块的名字本例为 HelloWorld dll             }

  Type a = typeof(Webtest ReflectTest);//得到具体的类的类型 和下面一个效果

  //Type a = t GetType( Webtest ReflectTest );//            //Console Write(a Name);

  string[] bb = { aaaa bbbbb };            object obj = Activator CreateInstance(a bb); //创建该类的实例 后面的bb为有参构造函数的参数            //object obj = t CreateInstance( Webtest ReflectTest );//与上面方法相同

  MethodInfo[] miArr = a GetMethods();            foreach (MethodInfo mi in miArr)           {                //Console Write(mi Name);  //显示所有的共有方法           }

  MethodInfo mi = a GetMethod( WriteString );//显示具体的方法            object[] aa= { 使用的是带有参数的非静态方法 };            string s = (string)mi Invoke(obj aa); //带参数方法的调用

  MethodInfo mi = a GetMethod( WriteName );            String[] aa = { 使用的是静态方法 };            string s = (string)mi Invoke(null aa ); //静态方法的调用

  MethodInfo mi = a GetMethod( WriteNoPara );            string s = (string)mi Invoke(obj null); //不带参数的方法调用

  MethodInfo mi = a GetMethod( WritePrivate BindingFlags Instance | BindingFlags NonPublic);            string s = (string)mi Invoke(obj null); //私有类型方法调用

  //Console Write(s );

  PropertyInfo[] piArr = a GetProperties(BindingFlags Instance | BindingFlags NonPublic | BindingFlags Public);            foreach (PropertyInfo pi in piArr)            {             //Console Write(pi Name);  //显示所有的方法            }

  PropertyInfo pi =a GetProperty( Writea );            //pi SetValue(obj Writea null);            //Console Write(pi GetValue(obj null));

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

  FieldInfo fi = a GetField( Write );            //Console Write(fi GetValue(obj));

  ConstructorInfo[] ci = a GetConstructors();            foreach (ConstructorInfo ci in ci )            {                //Console Write(ci ToString()); //获得构造函数的形式            }

  ConstructorInfo asCI = a GetConstructor(new Type[] { typeof(string) typeof(string) });            //Console Write(asCI ToString());

  Webtest interface obj = (Webtest interface )t CreateInstance( Webtest ReflectTest );            Webtest ReflectTest obj = (Webtest ReflectTest)t CreateInstance( Webtest ReflectTest );            //Console Write(obj add());典型的工厂模式

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

lishixinzhi/Article/program/net/201311/12496  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码