foreach (var item in assembly.GetModules()) //遍历类库的dll文件
{
Console.WriteLine(item.Name);
}
foreach (var item in assembly.GetTypes())//遍历类库的类型
{
Console.WriteLine(item.Name);
foreach (var act in item.GetMethods())//遍历类的方法
{
Console.WriteLine(act.Name);
}
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show1");//无参方法
method.Invoke(ob1, null);
}
{
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show2", new Type[] { typeof(int) });//有一个参方法
method.Invoke(ob1, new object[] { 12 });
}
{
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show3", new Type[] { typeof(int), typeof(string) });//有两个参方法
method.Invoke(ob1, new object[] { 12, "haha" });
}
{
//Type myType1 = assembly.GetType("WeiAi.DB.MysqlHelper.GenericMethod");
//object ob1 = Activator.CreateInstance(myType1);
//MethodInfo method = myType1.GetMethod("Show");
//MethodInfo methodNew= method.MakeGenericMethod(new Type[] { typeof(int),typeof(string),typeof(double) });
//methodNew.Invoke(ob1,new object[] { 12,"Jianghao",123456});
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show4");
MethodInfo methodNew = method.MakeGenericMethod(new Type[] { typeof(int) });//泛型方法
methodNew.Invoke(ob1, new object[] { 123123123 });
}
{
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show5");//静态方法
method.Invoke(null, null);
}
{
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show6");//带返回值的方法
object result = method.Invoke(ob1, null);
Console.WriteLine(result);
}
Person oPerson = (Person)Activator.CreateInstance(type);
oPerson.Id = 123;
oPerson.Name = "hai";
oPerson.Age = 18;
oPerson.Description = "1111111111111111";
// Person person = new Person();
foreach (var item in type.GetProperties())//属性
{
Console.WriteLine(item.Name);
Console.WriteLine(item.GetValue(oPerson));
if (item.Name.Equals("Id"))
{
item.SetValue(oPerson, 12);
}
if (item.Name.Equals("Name"))
{
item.SetValue(oPerson, "jianghao");
}
if (item.Name.Equals("Age"))
{
item.SetValue(oPerson, 22);
}
if (item.Name.Equals("Description"))
{
item.SetValue(oPerson, "hahahahaha");
}
Console.WriteLine(item.GetValue(oPerson));
}
foreach (var item in type.GetFields())//字段
{
Console.WriteLine(item.Name);
}