0

Im trying to use reflection to call an internal static method thats part of a non static class.

Example of the class:

public class Example
{
    internal static string ExampleMethod(string input, out string output)
    {
        output = String.Empty;
        return string.Empty;
    }
}

My attempt at reflection:

var dynMethod = typeof(Example).GetMethod("ExampleMethod", BindingFlags.NonPublic | BindingFlags.Static);

This line is throwing the exception:

Exception has been thrown by the target of an invocation
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
CathalMF
  • 9,705
  • 6
  • 70
  • 106
  • Is the one line of code the line that causes that exception? I don't see where you're invoking the method. –  Jan 11 '19 at 15:30
  • 1
    Look at the `.InnerException` of the `TargetInvocationException`. Might be a loader problem. – Jeroen Mostert Jan 11 '19 at 15:30
  • 1
    Does you `ExampleMethod` compile? That would surprise me. You have an uninitialized `out` parameter – Flydog57 Jan 11 '19 at 15:30
  • 1
    Internal is there specifically so you do **not** call that function. Like all other Compile time Constraints, it is there for your own good and/or compiler communication. Actually the fact that **Exception** is thrown, indicates to me that this is a additional protection the Classs writer made **against** Reflection. All .NET Code uses very specific Exception Subclasses. Exception is only used by fellow Enduser Programmers when they are too lazy to make their own Exception Heirarchy. – Christopher Jan 11 '19 at 15:32
  • @Flydog57 Thats a typo. I just added a bit of code as an example. Its not the actual code. Ill fix that. – CathalMF Jan 11 '19 at 15:35
  • 1
    It works just fine if you correct the `out` parameter identified by @Flydog57. I cannot reproduce the error based on the single line of code. –  Jan 11 '19 at 15:35
  • 2
    Thanks everyone. I actually found the problem. There is method override. The internal exception is "Ambiguous match found." – CathalMF Jan 11 '19 at 15:45
  • 2
    @Christopher: "Exception has been thrown by the target of an invocation" is the default message of `TargetInvocationException`. It has nothing to do with the type of the exception thrown. Also, there's nothing a library author can do to "protect" code from reflection, short of obfuscating it. There are no hooks you can install that will somehow fire when somebody is reflecting over code. – Jeroen Mostert Jan 11 '19 at 15:46
  • @JeroenMostert: But that looks like a StackTrace/ToString() result, not a message. Unless they did the worst thing and replicated the Stacktrace in the Message? – Christopher Jan 11 '19 at 15:48
  • @JeroenMostert, actually, you can. Since .NET 4.6 and for `private` members only. Look [here](https://stackoverflow.com/a/40630569/2846483). – dymanoid Jan 11 '19 at 15:54
  • @dymanoid: thanks for the pointer. A strange little feature; I wonder what the design rationale was. It will only offer any security at all in specific scenarios where the executing code is limited in other ways, since anyone with access to the assembly contents and process memory can still take it apart in other ways. – Jeroen Mostert Jan 11 '19 at 15:57

0 Answers0