如何调用从C#类的静态方法,而无需创建实例 [英] How to invoke static method from C# class without creating instance
本文介绍了如何调用从C#类的静态方法,而无需创建实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的代码:
class Program
{
static void Main(string[] args)
{
Assembly myAsm = Assembly.LoadFile(@"c:\Some.dll");
Type myService = myAsm.GetType("SomeClass");
String s = (String) myService.InvokeMember("SomeMethod", BindingFlags.InvokeMethod | BindingFlags.Public,
null, null, new object[] {"MyParam"});
}
}
在Some.Dll有公共静态方法用的someMethod字符串参数返回字符串
,但我得到的方法缺少错误...
In Some.Dll there is public static Method SomeMethod with String param returning String but I get method missing Error...
推荐答案
您缺少 BindingFlags.Static
标记。
String s = (String) myService.InvokeMember("SomeMethod", BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public,
null, null, new object[] {"MyParam"});
这篇关于如何调用从C#类的静态方法,而无需创建实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文