常用反射 方法


//程序集

 using System;
namespace Webtest
{
public interface interface1
{
int add();
}

public class ReflectTest:interface1
{
public string Write;
private string Writec;


public String Writea
{
get{ return this.Write;}
set{this.Write=value;}
}

private string Writeb
{
get{return this.Writec;}
set{this.Writec=value;}
}

public ReflectTest()
{
this.Write="Write";
this.Writec="Writec";
}

public ReflectTest(string str1,string str2)
{
this.Write=str1;
this.Writec=str2;
}

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 5;

}

}

}
/
///反射中常用的方法
///

using System;
using System.Threading;
using System.Reflection;
class Test
{
  delegate  string TestDelegate (string value,int value1);
  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","bbbb"};
     object obj=Activator.CreateInstance(a,bb);//创建该类的实例,后面的bb为有参构造函数的参数
     //object obj=t.CreateInstance("Webtest.ReflectTest");//与上面方法同
    
    
     //*******************************************************
     MethodInfo[] miArr=a.GetMethods();
     foreach(MethodInfo mio in miArr)
     {
       //Console.Write(mio.Name);//显示所有的共有方法
     }
     //*******************************************************
     MethodInfo mi=a.GetMethod("WriteString");//显示具体的方法
     object[] aa={"使用的是带有参数的非静态方法",2);
     string s=(string) mi.Invoke(obj,aa);//带参数方法的调用
    
     MethodInfo mi1=a.GetMethod("WriteName");
     string[] aa1="使用的是静态方法"};
     string s1=(string)mi1.Invoke(null,aa1);//静态方法的调用
    
     MethodInfo mi2=a.GetMethod("WriteNoPara");
     string s2=(string)mi2.Invoke(obj,null);//不带参数的方法调用
    
     MethodInfo mi3=a.GetMethod("WritePrivate",BindingFlags.Instance|BindingFlags.NoPublic);
     string s3=(string)mi3.Invoke(obj,null);
     //Console.Write(s3);
    
    //*******************************************************
       PropertyInfo[] piArr=a.GetProperties(BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);
       foreach(PropertyInfo pi in piArr)
       {
         //Console.Write(pi.Name);//显示所有的属性
       }
    //*******************************************************      
       PropertyInfo pi1=a.GetPeroperty("Writea");
       //pi1.SetValue(obj,"Writea",null);
       //Console.Write(pi1.GetValue(obj,null));
      
       PropertyInfo pi2=a.GetProperty("Writeb",BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);
       pi2.SetValue(obj,"Writeb",null);
       //Console.Write(pi2.GetValue(obj,null));
      
       FieldInfo fi1=a.GetField("Write"0;
       //Console.Write(fi1.GetValue(obj));
    //*******************************************************
       ConstructorInfo[] ci1=a.GetConstructors();
       foreach(ConstructorInfo ci in ci1)
       {
          //Console.Write(ci.ToString());//获得构造函数的形式
       }
      
       ConstructorInfo asCI=a.GetConstructor(new Type[]{typeof(string),typeof(string)}
       //Console.Write(asCI.ToString());
      
    //*******************************************************
       Webtest.interface1 obj1=(Webtest.interface1)t.CreateInstance("Webtest.ReflectTest");
       Webtest.ReflectTest obj2=(Webtest.ReflectTest)t.CreateInstance("Webtest.ReflectTest");
   //*******************************************************
       foreach(Type tt in t.GetTypes())
       {
          if(tt.GetInterface("interface1")!=null)
          {
            Webtest.interface1 obj3=(Webtest.interface1)Activator.CreateInstance(a);
            //Console.Write(obj3.add());
          }
       }
   //*******************************************************
      TestDelegate method=(TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate),obj,"WriteString");
      //动态创建委托的简单例子
      //Console.Write(method("str1",2))  
     
  //*******************************************************     
 
     ConstructorInfo asCI1=a.GetConstructor(new Type[0]);
     Webtest.ReflectTest obj5=(Webtest.ReflectTest)asCI1.Inoke(null);
     //通过无参构造函数实例化的方法
     //Console.Write(obj5.Writea);
    
     ConstructorInfo asCI2=a.GetConstructor(new Type[] {typeof(string),typeof(string)});
     //通过有参构造函数实例化的方法
     Webtest.ReflectTest obj6=(Webtest.ReflectTest)asCI2.Inoke(bb);
     Console.Write(obj6.Writea);
 //********************************************************
     Console.Read();
     }
   }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
         
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值