namespace IWebTest
{
interface interface1
{
int add();
}
public class Class1:interface1
{
private string _write;
public string _writec;
public Class1(string a)
{
this._write = a;
}
public Class1()
{
this._write = "noparam";
}
public string write
{
get { return _write; }
set { _write = value; }
}
public string writec
{
get { return _writec; }
set { _writec = value; }
}
public void ReflectTest()
{
this._write = "write";
this._writec = "writec";
}
public string ReflectTest(string a,string b)
{
this._write=a;
this._writec=b;
return _write + _writec;
}
public string WriteString(string a, int b)
{
return "welcome you " + a + "----"+b.ToString();
}
public static string WriteName(string name)
{
return "welcome " + name;
}
public string WriteNoParam()
{
return "no param";
}
private string WritePrivate()
{
return "write private way";
}
public int add()
{
return 5;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using IWebTest;
namespace ConsoleApplication1
{
class Program
{
delegate string TestDelegate(string value, string value1);
static void Main(string[] args)
{
//得到程序集
Assembly t = Assembly.Load("IWebTest");
//列出所有的类型,类,接口....
foreach (Type aaa in t.GetTypes())
{
Console.Write(aaa.Name);
Console.WriteLine("next");
}
//list modules(.dll files)
Module[] modules = t.GetModules();
foreach (Module bbb in modules)
{
Console.Write(bbb.Name);
Console.WriteLine("next module");
}
//create instance across constructor
Type a = t.GetType("IWebTest.Class1");
Console.Write(a.Name);
foreach (MethodInfo x in a.GetMethods())
{
Console.WriteLine(x.Name);
}
object[] obj = new object[] { "aaaaaaaa" };
object dobj = Activator.CreateInstance(a, obj);
//Get way of instance
MethodInfo method = a.GetMethod("WriteName");
object[] param = new object[] { "mary" };
//invoke way
object returnvalue = method.Invoke(dobj, param);
Console.WriteLine(returnvalue.ToString());
//MethodInfo add = a.GetMethod("add");
//Console.WriteLine(add.Invoke(dobj,null));
//invoke static way
MethodInfo smt = a.GetMethod("WriteName");
object[] objsmt = new object[] {"static way invoke" };
Console.WriteLine((string)smt.Invoke(null, objsmt));
//invoke way no param
MethodInfo noparam = a.GetMethod("WriteNoParam");
Console.WriteLine((string)noparam.Invoke(dobj, null));
MethodInfo priway = a.GetMethod("WritePrivate", BindingFlags.Instance | BindingFlags.NonPublic);
Console.WriteLine((string)priway.Invoke(dobj, null));
//get property
PropertyInfo[] proall = a.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo p in proall)
Console.WriteLine(p.Name);
PropertyInfo p1 = a.GetProperty("write");
p1.SetValue(dobj,"_gggggggggggggggggge",null);
Console.WriteLine(p1.GetValue(dobj,null));
//Get field
FieldInfo finfo = a.GetField("_write",BindingFlags.Instance | BindingFlags.NonPublic);
Console.WriteLine(finfo.GetValue(dobj));
//Get Constructor Information
ConstructorInfo[] conInfo = a.GetConstructors();
foreach (ConstructorInfo cInfoa in conInfo)
Console.WriteLine(cInfoa.ToString());
ConstructorInfo conInfoa=a.GetConstructor(new Type[]{typeof(string)});
Console.WriteLine(conInfoa.ToString());
Console.WriteLine(conInfoa.Name);
TestDelegate tDele = (TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate), dobj, "ReflectTest");
Console.WriteLine(tDele("aaaaaaaaaa", "bbbbbbbbbbb"));
}
}
}