在网上查找了不少的资料可以说大同小异概念性的东西网上一搜一堆今天把反射的东西整理了一下供大家使用我保证我这里是最全面的东西当然也是基础的东西在学好了这一切的基础上大家可以学习反射的具体插件等应用老鸟就不用看了首先我们建立一个类库将它生成为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();
}
}
在这里我把我们常用的方法属性等全部整理了出来大家不要嫌弃乱静下心来自己按照我的分隔一部分一部分的来保证你对反射的学习会事半功倍当然有关于其方法我会继续补充想了这么些就先写下来吧