此篇将介绍C#如何在运行时动态调用方法。当某些类型是运行时动态确定时,编译时的静态编码是无法解决这些动态对象或类的方法调用的。此篇则给你一把利剑,让动态对象的方法调用成为可能。
1.动态调用dll里的方法
/// <summary>
/// 该类将被独立编入Class1.dll汇编
/// </summary>
class Class1
{
public static string method1()
{
return "I am Static method (method1) in class1";
}
public string method2()
{
return "I am a Instance Method (method2) in Class1";
}
public string method3(string s)
{
return "Hello " + s;
}
}
/// <summary>
/// 该类独立放入Test.exe汇编
/// </summary>
class DynamicInvoke
{
public static void Main(string[] args)
{
// 动态加载汇编
string path = "Class1.dll";
Assembly assembly = Assembly.Load(path);
// 根据类型名得到Type
Type type = assembly.GetType("Class1");
// 1.根据方法名动态调用静态方法
string str = (string)type.InvokeMember("method1", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, new object[] { });
Console.WriteLine(str);
// 2.根据方法名动态调用动态对象的成员方法
object o = Activator.CreateInstance(type);
str = (string)type.InvokeMember("method2", BindingFlags.Default | BindingFlags.InvokeMethod, null, o, new object[] { });
Console.WriteLine(str);
// 3.根据方法名动态调用动态对象的有参成员方法
object[] par = new object[] { "kunal" };
str = (string)type.InvokeMember("method3", BindingFlags.Default | BindingFlags.InvokeMethod, null, o, par);
Console.WriteLine(str);
// 带out修饰的InvokeMember
// System.Int32 中 public static bool TryParse(string s, out int result) 方法的调用
var arguments = new object[] { str, null }; // 注意这里只能将参数写在外面,out参数为null也没有关系
typeof(int).InvokeMember("TryParse", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Static,
null, null, arguments);
Console.WriteLine(arguments[1]);
}
}
2.动态加载类文件并调用方法:
using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using Microsoft.CSharp;
namespace _32.DynamicReflection
{
internal class Program
{
private static void Main(string[] args)
{
#region 内置标签方法 (动态加载)
const string className = "DynamicReflection.Test"; //类名称一定要全称
string fileName = Thread.GetDomain().BaseDirectory + "Test.cs";
if (File.Exists(fileName))
{
var sourceFile = new FileInfo(fileName);
CodeDomProvider provider = new CSharpCodeProvider();
var cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.dll"); //添加命名空间引用
cp.GenerateExecutable = false; // 生成类库
cp.GenerateInMemory = true; // 保存到内存
cp.TreatWarningsAsErrors = false; // 不将编译警告作为错误
// 编译
CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceFile.FullName);
if (cr.Errors.Count < 1)
{
Assembly asm = cr.CompiledAssembly; // 加载
//1.调用静态方法
Type type = asm.GetType(className);
var str =(string)type.InvokeMember("SayHello1", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, new object[] {});
Console.WriteLine(str);
//2.调用实例方法
object instance = asm.CreateInstance(className);
str =(string)type.InvokeMember("SayHello2", BindingFlags.Default | BindingFlags.InvokeMethod, null, instance,new object[] {});
Console.WriteLine(str);
//3.调用带参数的方法
var par = new object[] {"zhangqs008"};
str =(string)type.InvokeMember("SayHello3", BindingFlags.Default | BindingFlags.InvokeMethod, null, instance,par);
Console.WriteLine(str);
Console.Read();
}
else
{
string msg = null;
for (int index = 0; index < cr.Errors.Count; index++)
{
CompilerError error = cr.Errors[index];
msg += "【错误" + (index + 1) + "】" + Environment.NewLine;
msg += "[文件] " + error.FileName + Environment.NewLine;
msg += "[位置] 行" + error.Line + ",列" + error.Column + Environment.NewLine;
msg += "[信息] " + error.ErrorText + Environment.NewLine;
msg += Environment.NewLine;
}
MessageBox.Show(msg, "内置方法类编译错误");
}
}
#endregion
}
}
}
类文件:
namespace DynamicReflection
{
public class Test
{
public static string SayHello1()
{
return "hello static method";
}
public string SayHello2()
{
return "hello instance method";
}
public string SayHello3(string args)
{
return "hello args " + args;
}
}
}