本文主要是介绍C# 利用类名字符串调用并执行类方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
/// <summary>
/// 调用并执行指定类里面的函数
/// </summary>
/// <param name="className">需要调用的类名(包含其命名空间)</param>
/// <param name="methodName">需要调用的方法名</param>
/// <param name="parameters">传递的参数值</param>
public void GetAndExecuteMethod(string className, string methodName, object[] parameters=null)
{
try
{
var type = Type.GetType(className);
if (type == null)
throw new NullReferenceException("类" + className + "不存在");
var obj = type.Assembly.CreateInstance(className);
//调用其方法
var method = type.GetMethod(methodName);
if(method == null)
throw new NullReferenceException("方法" + methodName + "不存在");
//执行方法
method.Invoke(obj, parameters);
}
catch (Exception)
{
throw;
}
}
这篇关于C# 利用类名字符串调用并执行类方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!