假设我有一个具有以下代码的ILAsm库:
.assembly extern mscorlib{}
.assembly TestMe{}
.module TestMe.dll
.method public static void PrintMe() cil managed
{
ldstr "I'm alive!"
call void [mscorlib]System.Console::WriteLine(string)
ret
}如何从PrintMe代码调用全局C# ()函数?
发布于 2013-10-05 11:31:46
你可以通过反射..。但不是以任何真正有用的方式:
var module = Assembly.LoadFile("path_to_dll.dll").GetModules()[0];
// the first and only module in your assembly
var method = module.GetMethod("PrintMe");
method.Invoke(null,
BindingFlags.Public | BindingFlags.Static,
null,
null,
CultureInfo.CurrentCulture); // Prints "I'm alive!" to console.发布于 2013-10-05 11:02:34
据我所知你不能。C#只“知道”在类型中声明的方法--而不是在顶层。将方法移动到类型中。
https://stackoverflow.com/questions/19196913
复制相似问题