此篇将介绍C#如何在运行时动态调用方法。当某些类型是运行时动态确定时,编译时的静态编码是无法解决这些动态对象或类的方法调用的。此篇则给你一把利剑,让动态对象的方法调用成为可能。

1.动态调用dll里的方法

  1. <span style="font-family:SimSun;font-size:12px;">/// <summary>
  2. /// 该类将被独立编入Class1.dll汇编
  3. /// </summary>
  4. class Class1
  5. {
  6. public static string method1()
  7. {
  8. return "I am Static method (method1) in class1";
  9. }
  10. public string method2()
  11. {
  12. return "I am a Instance Method (method2) in Class1";
  13. }
  14. public string method3(string s)
  15. {
  16. return "Hello " + s;
  17. }
  18. }
  19. /// <summary>
  20. /// 该类独立放入Test.exe汇编
  21. /// </summary>
  22. class DynamicInvoke
  23. {
  24. public static void Main(string[] args)
  25. {
  26. // 动态加载汇编
  27. string path = "Class1.dll";
  28. Assembly assembly = Assembly.Load(path);
  29. // 根据类型名得到Type
  30. Type type = assembly.GetType("Class1");
  31. // 1.根据方法名动态调用静态方法
  32. string str = (string)type.InvokeMember("method1", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, new object[] { });
  33. Console.WriteLine(str);
  34. // 2.根据方法名动态调用动态对象的成员方法
  35. object o = Activator.CreateInstance(type);
  36. str = (string)type.InvokeMember("method2", BindingFlags.Default | BindingFlags.InvokeMethod, null, o, new object[] { });
  37. Console.WriteLine(str);
  38. // 3.根据方法名动态调用动态对象的有参成员方法
  39. object[] par = new object[] { "kunal" };
  40. str = (string)type.InvokeMember("method3", BindingFlags.Default | BindingFlags.InvokeMethod, null, o, par);
  41. Console.WriteLine(str);
  42. // 带out修饰的InvokeMember
  43. // System.Int32 中 public static bool TryParse(string s, out int result) 方法的调用
  44. var arguments = new object[] { str, null }; // 注意这里只能将参数写在外面,out参数为null也没有关系
  45. typeof(int).InvokeMember("TryParse", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Static,
  46. null, null, arguments);
  47. Console.WriteLine(arguments[1]);
  48. }
  49. }</span>

2.动态加载类文件并调用方法:

  1. <span style="font-family:SimSun;font-size:12px;">using System;
  2. using System.CodeDom.Compiler;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Threading;
  6. using System.Windows.Forms;
  7. using Microsoft.CSharp;
  8. namespace _32.DynamicReflection
  9. {
  10. internal class Program
  11. {
  12. private static void Main(string[] args)
  13. {
  14. #region 内置标签方法 (动态加载)
  15. const string className = "DynamicReflection.Test"; //类名称一定要全称
  16. string fileName = <strong>Thread.GetDomain().BaseDirectory + "Test.cs";</strong>
  17. if (File.Exists(fileName))
  18. {
  19. var sourceFile = new FileInfo(fileName);
  20. CodeDomProvider provider = new CSharpCodeProvider();
  21. var cp = new CompilerParameters();
  22. cp.ReferencedAssemblies.Add("System.dll"); //添加命名空间引用
  23. cp.GenerateExecutable = false; // 生成类库
  24. cp.GenerateInMemory = true; // 保存到内存
  25. cp.TreatWarningsAsErrors = false; // 不将编译警告作为错误
  26. // 编译
  27. CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceFile.FullName);
  28. if (cr.Errors.Count < 1)
  29. {
  30. Assembly asm = cr.CompiledAssembly; // 加载
  31. //1.调用静态方法
  32. Type type = asm.GetType(className);
  33. var str =(string)type.InvokeMember("SayHello1", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, new object[] {});
  34. Console.WriteLine(str);
  35. //2.调用实例方法
  36. object instance = asm.CreateInstance(className);
  37. str =(string)type.InvokeMember("SayHello2", BindingFlags.Default | BindingFlags.InvokeMethod, null, instance,new object[] {});
  38. Console.WriteLine(str);
  39. //3.调用带参数的方法
  40. var par = new object[] {"zhangqs008"};
  41. str =(string)type.InvokeMember("SayHello3", BindingFlags.Default | BindingFlags.InvokeMethod, null, instance,par);
  42. Console.WriteLine(str);
  43. Console.Read();
  44. }
  45. else
  46. {
  47. string msg = null;
  48. for (int index = 0; index < cr.Errors.Count; index++)
  49. {
  50. CompilerError error = cr.Errors[index];
  51. msg += "【错误" + (index + 1) + "】" + Environment.NewLine;
  52. msg += "[文件] " + error.FileName + Environment.NewLine;
  53. msg += "[位置] 行" + error.Line + ",列" + error.Column + Environment.NewLine;
  54. msg += "[信息] " + error.ErrorText + Environment.NewLine;
  55. msg += Environment.NewLine;
  56. }
  57. MessageBox.Show(msg, "内置方法类编译错误");
  58. }
  59. }
  60. #endregion
  61. }
  62. }
  63. }</span>

类文件:

    1. <span style="font-family:SimSun;font-size:12px;">namespace DynamicReflection
    2. {
    3. public class Test
    4. {
    5. public static string SayHello1()
    6. {
    7. return "hello static method";
    8. }
    9. public string SayHello2()
    10. {
    11. return "hello instance method";
    12. }
    13. public string SayHello3(string args)
    14. {
    15. return "hello args " + args;
    16. }
    17. }
    18. }
    19. </span>

C#动态方法调用 提高程序的扩展性的更多相关文章

  1. struts2.3.15.3中动态方法调用默认是关闭的

    初学ssh,用的struts2.3.15.3,使用了如下表单: <form action="/spring3/index/login.action" method=" ...

  2. 第三章Struts2 Action中动态方法调用、通配符的使用

    01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...

  3. Struts2 动态方法调用

    01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...

  4. Struts2学习第二天——动态方法调用

    method属性 在前面的例子里,Action默认使用execute()方法来处理请求.但是,如果有多个不同的请求需要同一个Action进行不同处理,怎么办?在Struts.xml文件中,需要指定Ac ...

  5. JavaWeb_(Struts2框架)struts.xml核心配置、动态方法调用、结果集的处理

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

  6. Struts2学习笔记 - Action篇<动态方法调用>

    有三种方法可以使一个Action处理多个请求 动态方法调用DMI 定义逻辑Acton 在配置文件中使用通配符 这里就说一下Dynamic Method nvocation ,动态方法调用,什么是动态方 ...

  7. struts之动态方法调用使用通配符

    一.DMI动态方法调用的其中一种改变form表单中action属性的方式已经讲过了.还有两种,一种是改变struts.xml配置文件中action标签中的method属性,来指定执行不同的方法处理不同 ...

  8. struts之动态方法调用改变表单action属性

      一.动态方法调用(DMI:Dynamic Method Invocation) ⒈struts2中同样提供了这个包含多个逻辑业处理的Action,这样就可以在一个Action中进行多个业务逻辑处理 ...

  9. struts2DMI(动态方法调用)

    struts2动态方法调用共有三种方式: 1.通过action元素的method属性指定访问该action时运行的方法 <package name="action" exte ...

随机推荐

  1. 搭建Selenium环境

    1.下载并安装Python 此学习笔记使用Python语言进行开发,所以我已经安装了Python环境,我的Python版本为3.5.2: 2.安装selenium 因为我使用的Python3版本,在该 ...

  2. Spring boot进阶-配置Controller、interceptor...

    1.配置SpringBootApplication(对spring boot来说这是最基本) package io.github.syske.springboot31; import org.spri ...

  3. java大数据批量处理实现方式

    1. 各批量方式对比 Mybatis与JDBC批量插入MySQL数据库性能测试及解决方案 2. 原理解析 1)MySql PreparedStatement executeBatch过慢问题 3. 工 ...

  4. 洛谷P4459/loj#2511 [BJOI2018]双人猜数游戏(博弈论)

    题面 传送门(loj) 传送门(洛谷) 题解 所以博弈论的本质就是爆搜么-- 题解 //minamoto #include<bits/stdc++.h> #define R registe ...

  5. 控制台解析命行C#

    //---------------------------------------------------------------------   /// <summary> /// Co ...

  6. js 简单数据类型和复杂数据类型的区别

    原始数据类型: number,string,boolean,undefined, null,object 基本类型(简单类型),值类型: number,string,boolean 复杂类型(引用类型 ...

  7. mysql 彻底解决:Incorrect string value: '\xF0\x9F\x98\xAD",...' for column 'commentContent' at row 1

    彻底解决:Incorrect string value: '\xF0\x9F\x98\xAD",...' for column 'commentContent' at row 1 今天在爬取 ...

  8. 用 ASP.NET MVC 实现基于 XMLHttpRequest long polling(长轮询) 的 Comet

    ASP.NET 计时器   http://www.cnblogs.com/dudu/archive/2011/10/17/2215321.html   http://www.cnblogs.com/w ...

  9. leetcode 88 Merge Sorted Array 归并排序

    归并排序:先将数组一分为二,将左边部分排序(同样将其一分为二),再将右边部分排序,最后逐层归并.(分治策略)(稳定排序). 算法稳定性 -- 假设在数列中存在a[i]=a[j],若在排序之前,a[i] ...

  10. 最小生成树----prim算法的堆优化

    题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入输出格式 输入格式: 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M<= ...