8

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});
2
  • 4
    AFAIK, the internal access modifier intent is this: not to let you do it. =) Commented Aug 21, 2012 at 20:38
  • @HillBilly.Developer Yes, you can. Extension methods are still regular static methods, and can be called using the regular syntax. The extension method syntax is optional, and additive. Commented Aug 21, 2012 at 20:42

2 Answers 2

9

You are passing an array of strings as a single parameter with your current code.

Since string[] can be coerced to object[], you can just pass the parameters array into Invoke.

string result = (string)mi.Invoke(null, parameters);
Sign up to request clarification or add additional context in comments.

4 Comments

@AndreCalil Unless this is a metro application, yes you can call internal or private classes and methods via reflection. Access modifiers are not a security precaution.
@AndreCalil, yes - you can call anything (including private methods with arguments as private classes) via reflection. (Assuming Remko got object/method info correct).
@AlexeiLevenkov Nice, I thought that reflection would respect access modifiers, though.
@AndreCalil technically, reflection permission is required to access non public members (public members are always available via reflection), but this is not something an assembly author can decide. It depends on the security context of the calling code. For example if I run my client code as admin then you there is nothing your library can do to prevent me from using reflection. However client code can create a sandbox to restrict other code from using reflection APIs. msdn.microsoft.com/en-us/library/stfy7tfc(v=vs.71).aspx
3

If you need this for testing purposes consider using InternalsVisibleTo attribute. This way you can make your test assembly to be "friend" of main assembly and call internal methods/classes.

If you don't have control (or can't sign) assemblies - reflection is the way to do it if you have to. Calling internal methods of third party assemblies is good way to get headaches when that assembly changes in any shape of form.

1 Comment

Yes I am aware of that risk but it's acceptable for my usage (+1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.