zoukankan      html  css  js  c++  java
  • C# 程序集反射

    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";
                }
            }
        }
    }
  • 相关阅读:
    异地协作,A地上传jar包到B地服务器上传速率慢
    linux一行命令查杀进程
    maven项目创建.m2文件夹
    模态框传递参数
    测试身份证信息
    jenkins:邮件配置良心之作
    python:不错的python编程核心思想
    jenkins:忘记密码怎么办
    docker:如何查看容器的挂载目录
    JavaScript + PHP 实现刷新继续保持倒计时的按钮
  • 原文地址:https://www.cnblogs.com/rinack/p/4260016.html
Copyright © 2011-2022 走看看