C# 程序集反射
2015-01-29 15:31
225 查看
namespace AssemblyLibrary { public class AssemblyLibrary { public static object LoadAssembly(string filePath,string nameSpace,string typeName,string methodName,object[] parameters) { try { byte[] filesByte = File.ReadAllBytes(filePath); Assembly assembly = Assembly.Load(filesByte); System.Type[] types = assembly.GetTypes(); foreach (System.Type dllType in types) { if (dllType.Namespace == nameSpace && dllType.Name == typeName) { Type type = assembly.GetType(nameSpace + "." + typeName); object obj = System.Activator.CreateInstance(type); MethodInfo methodInfo = type.GetMethod(methodName); if (methodInfo != null) { return methodInfo.Invoke(obj, parameters); } } } return null; } catch (Exception ex) { throw ex; } } } }
private void btnExecte_Click(object sender, EventArgs e) { try { if (this.txtPathFile.Text.Trim() == "") { MessageBox.Show("请输入相关查询条件!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Question); return; } object obj = AssemblyLibrary.AssemblyLibrary.LoadAssembly(this.txtPathFile.Text,"TestLibrary", "TestLibrary", "GetMethod", new object[] { this.txtParameters.Text }); if (obj != null) { MessageBox.Show(obj.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Question); } } catch (Exception ex) { MessageBox.Show(ex.Message, "异常提示", MessageBoxButtons.OK, MessageBoxIcon.Question); } }
namespace TestLibrary { public class TestLibrary { public string GetMethod(string parm) { switch (parm) { case "1": return "11111111"; case "2": return "22222222"; case "3": return "33333333"; default: return "00000000"; } } } }
相关文章推荐
- C#中使用反射显示程序集的所有类型和属性(转贴)
- C#中通过反射查看程序集的特性信息
- C#反射 获取程序集信息和通过类名创建类实例
- C# 程序集 和 反射
- C# 程序集反射
- C#2.0学习8--反射与程序集
- C#使用反射加载多个程序集
- C#利用反射来加载程序集,并调用程序集中有关类的方法
- 《CLR via C#》笔记——程序集的加载和反射(3)
- C#中的程序集和反射
- C#中使用反射显示程序集的所有类型和属性
- C# 如何利用反射来加载程序集,并调用程序集中有关类的方法
- C#之反射与程序集
- C# 如何利用反射来加载程序集,并调用程序集中有关类的方法【转】
- C#中使用反射显示程序集的所有类型和属性
- C#中使用反射显示程序集的所有类型和属性
- C#反射创建对象(根据程序集)
- c#使用反射加载程序集Assembly。load()使用全过程
- c#的反射中及命名空间程序集的获取
- C# 程序集 与 反射