本文将反射的东西整理了一下 , 提供了最全面的东西 , 当然也是基础的东西 ,
在学好了这一切的基础上 , 大家可以学习反射的具体插件等应用 首先我们建立一个类库 , 将它生成为 reflectPrj .dll,
using System.Collections.Generic;
using System.Text;
namespace reflectPrj
{
/// <summary>
/// 接口
/// </summary>
public interface Interface1
{
int Add(int num);
int Add();
}
/// <summary>
/// 用来被测试的类
/// </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 " 你使用的是无参数方法!" ;
}
private string WritePrivate()
{
return " 私有类型的方法!" ;
}
}
}</div>
之后再建立一个项目引入该 reflectPrj .dll
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------ 创建类型的实例----> 此处是由带参的构造函数的来得到的实例
string [] paras ={"aaa" ,"bbb" };
// 创建该类的实例,后面的paras 为有参构造函数的参数----> 此obj