如何动态调用方法

如何动态调用方法

本文介绍了如何动态调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个我想动态调用的第三方程序集.

意味着我仅在存在第三方程序集时才需要调用类的方法. (该应用程序被部署为一次单击部署)

谢谢
/Amaw Scritz

There is a third party assembly which I would like to call dynamically.

Meaning I need to call a method of a class only when that third party assembly is present. (The application is deployed as a click-once deployment)

Thanks
/Amaw Scritz

推荐答案

var t = Type.GetType("the full name of your class here");

if (t != null)
{
     // the type is present so your assembly is there
     var methodToCall = t.GetMethod("Name of the method to call");
     // invocation
     var result = methodToCall.Invoke(<targetornullifstaticmethod>, <parametersasanarrayofobjects>);

}


targetOrNullIfStaticMethod是要在其上调用方法的对象实例
parametersAsAnArrayOfObjects是方法期望的参数数组(如果没有参数,则为null)


targetOrNullIfStaticMethod being the object instance on which you want to call the method
parametersAsAnArrayOfObjects being an array of the parameters the method is expecting (or null if no parameters)


这篇关于如何动态调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 00:26