c# 反射调用程序集方法、接口实例
来源:互联网 发布:安装三菱plc编程软件 编辑:程序博客网 时间:2024/05/29 18:33
新建类库:Webtest
添加相应类:ReflectTest 接口interface1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Webtest
{
public interface interface1
{
int add();
}
public class ReflectTest : interface1
{
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()
{
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 "/n这是类库函数打印的字符串:欢迎您," + s + "---" + b; ;
}
public static string WriteName(string s)
{
return "/n这是类库函数打印的字符串:欢迎您光临," + s;
}
public string WriteNoPara()
{
return "/n这是类库函数打印的字符串:您使用的是无参数方法";
}
private string WritePrivate()
{
return "/n这是类库函数打印的字符串:私有类型的方法";
}
public int add()
{
return 5;
}
}
}
新建测试控制台项目,并添加引用
using System;
using System.Reflection;
namespace Test
{
class Test
{
delegate string TestDelegate(string value, int value1);
static void Main()
{
//Assembly t = Assembly.LoadFrom("Webtest.dll"); //与下面相同的效果
Assembly t = Assembly.Load("Webtest");
Console.Write("Webtest类库里有以下类:/n");
foreach (Type aaa in t.GetTypes())
{
Console.Write(aaa.Name + "/n"); //显示该dll下所有的类
}
Module[] modules = t.GetModules();
Console.WriteLine();
foreach (Module module in modules)
{
Console.WriteLine("模块名称:/n" + module.Name);//显示模块的名字本例为"HelloWorld.dll"
}
Type a = typeof(Webtest.ReflectTest);//得到具体的类的类型,和下面一个效果
//Type a = t.GetType("Webtest.ReflectTest");
//Console.Write("模块里的类有:" + a.Name + "/n");
string[] bb = { "aaaaa", "bbbbb" };
object obj = Activator.CreateInstance(a, bb); //创建该类的实例,后面的bb为有参构造函数的参数
//object obj = t.CreateInstance("Webtest.ReflectTest");//与上面方法相同
//获得实例公共方法的集合
MethodInfo[] miArr = a.GetMethods();
Console.Write("/n共有方法: /n");
foreach (MethodInfo mi0 in miArr)
{
Console.Write(mi0.Name + "/n"); //显示所有的共有方法
}
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.NonPublic);
string s3 = (string)mi3.Invoke(obj, null); //私有类型方法调用
//Console.Write(s3);
//获得实例公共属性的集合
PropertyInfo[] piArr = a.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
Console.WriteLine("/n显示所有的属性:");
foreach (PropertyInfo pi in piArr)
{
Console.WriteLine(pi.Name); //显示所有的属性
}
PropertyInfo pi1 = a.GetProperty("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");
//Console.Write(fi1.GetValue(obj));
//获得实例公共构造函数的集合
ConstructorInfo[] ci1 = a.GetConstructors();
Console.WriteLine("/n显示所有的构造函数:");
foreach (ConstructorInfo ci in ci1)
{
Console.Write(ci.ToString() + "/n"); //获得构造函数的形式
}
//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");
//Console.Write(obj1.add());典型的工厂模式
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));
Console.Read();
}
}
}
- c# 反射调用程序集方法、接口实例
- C#反射方法:程序集反射、类型反射、接口反射、类型成员反射
- C#反射实例调用
- C# 反射-不需要接口约束,调用各种方法
- C#利用反射来加载程序集,并调用程序集中有关类的方法
- 小程序调用接口实例
- 小程序调用接口实例
- C#反射调用泛型类方法
- C#反射调用私有方法
- C# 反射详解:定义、创建对象、调用实例方法及静态方法
- C# 完整的反射(调用程序集)
- C# 反射-通过继承接口实现创建对象并调用方法
- C# 程序集反射
- 【C#反射程序集】
- C#调用URL接口方法
- C#反射动态调用窗体,动态实例化窗体的方法
- C#反射实例化类并调用带参数的方法
- java 反射入门-属性方法调用实例
- UML核心元素之包
- 编程之美3.7
- json解析例子
- MySQL group_concat函数进行行转列使用
- Android学习笔记:设置日期和时间不能选择当前时刻以前的日期时间的解决办法
- c# 反射调用程序集方法、接口实例
- pl/sql developer 中文乱码
- 常用批处理命令之For命令详解
- ubuntu12.04里面安装mysql
- 个人常用eclipse快捷键,不断更新
- POSIX thread (pthread) 简介
- 欧洲核子研究中心公布强子对撞机事故初步分析
- 托管和非托管代码
- Java内存泄露原因详解