C#调用在运行时没有构建时参考的静态方法? [英] C# call a static method at runtime without a build time reference?
问题描述
我写在C#.NET系统(2.0)。它有一个可插拔模块排序架构。组件可添加到系统,而不重建基本模块。为了使新模块的连接,我想试图通过名字来称呼一个静态方法在其他模块。我不想叫模块在构建时的任何方式进行引用。
I am writing a system in C# .net (2.0). It has a pluggable module sort of architecture. Assemblies can be added to the system without rebuilding the base modules. To make a connection to the new module, I wish to attempt to call a static method in some other module by name. I do not want the called module to be referenced in any manner at build time.
回来时,我正在写从路径开始非托管代码到.dll文件我会使用调用LoadLibrary()来获取.dll文件到内存中,然后使用得到GetProcAddress的()获得一个指向我想调用该函数。我如何实现在C#/。NET相同的结果。
Back when I was writing unmanaged code starting from the path to the .dll file I would use LoadLibrary() to get the .dll into memory then use get GetProcAddress() get a pointer to the function I wished to call. How do I achieve the same result in C# / .NET.
推荐答案
装配使用Assembly.LoadFrom(加载后.. ),你可以按名称的类型和得到任何静态方法:
After the assembly is loaded using Assembly.LoadFrom(...), you can get the type by name and get any static method:
Type t = Type.GetType(className);
// get the method
MethodInfo method = t.GetMethod("MyStaticMethod",BindingFlags.Public|BindingFlags.Static);
Then you call the method:
method.Invoke(null,null); // assuming it doesn't take parameters
这篇关于C#调用在运行时没有构建时参考的静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!