A function call inside an inner class from another executable

I want to call a function from an .net executable from my own code. I used a reflector and saw this:

namespace TheExe.Core { internal static class AssemblyInfo internal static class StringExtensionMethods } 

In the TheExe.Core namespace, this is the function that interests me:

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

Using this code, I see a Hash method, but what should 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}); 
+7
source share
2 answers

You pass an array of strings as one parameter with your current code.

Since string[] can be forced to object[] , you can simply pass the parameters array to Invoke .

 string result = (string)mi.Invoke(null, parameters); 
+9
source

If you need this for testing, use the InternalsVisibleTo attribute. Thus, you can make your test assembly โ€œfriendโ€ of the main assembly and call internal methods / classes.

If you do not have control (or cannot subscribe) of assemblies - reflection is a way to do this if you need to. Calling internal methods of third-party assemblies is a good way to get headaches when this assembly changes in any form.

+3
source

All Articles