I want to call a function from a .net executable from my own code.
I used reflector and saw this:
namespace TheExe.Core
{
internal static class AssemblyInfo
internal static class StringExtensionMethods
}
In namespace TheExe.Core is the function I am interested in:
internal static class StringExtensionMethods
{
// Methods
public static string Hash(this string original, string password);
// More methods...
}
Using this code I can see the Hash Method but how do I call it?
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});
You are passing an array of strings as a single parameter with your current code.
Since
string[]can be coerced toobject[], you can just pass theparametersarray intoInvoke.