从另一个可执行文件调用内部类中的函数

Rem*_*mko 7 .net c# reflection

我想从我自己的代码中调用.net可执行文件中的函数.我用反射器看到了这个:

namespace TheExe.Core
{
    internal static class AssemblyInfo
    internal static class StringExtensionMethods
}
Run Code Online (Sandbox Code Playgroud)

在命名空间中,TheExe.Core是我感兴趣的函数:

internal static class StringExtensionMethods
{
    // Methods
    public static string Hash(this string original, string password);
    // More methods...
}
Run Code Online (Sandbox Code Playgroud)

使用此代码我可以看到哈希方法,但我该如何调用它?

Assembly ass = Assembly.LoadFile("TheExe");
Type asmType = ass.GetType("TheExe.Core.StringExtensionMethods");
MethodInfo mi = asmType.GetMethod("Hash", BindingFlags.Public | BindingFlags.Static);
string[] parameters = { "blabla", "MyPassword" };

// This line gives System.Reflection.TargetParameterCountException
// and how to cast the result to string ?
mi.Invoke(null, new Object[] {parameters});
Run Code Online (Sandbox Code Playgroud)

Mik*_*ray 9

您将使用当前代码将字符串数组作为单个参数传递.

既然string[]可以强制执行object[],你可以将parameters数组传递给Invoke.

string result = (string)mi.Invoke(null, parameters);
Run Code Online (Sandbox Code Playgroud)

  • 你确定它会起作用吗?那个班是'内部' (2认同)