none
MethodInfo calling private methods returning a null RRS feed

  • Question

  • Hi All,

    I am having an issue with the code below. When I run it, it returns a null when i try to invoke the printmethod2. It seems to only happen when I am trying to invoke a private method (SomeMethod2), if I try and invoke "SomeMthod", which is public, it runs fine.

    Any ideas?

    namespace ConsoleApp1
    {
        class Program
        {
            internal static String AssembliesPath
            {
                get
                {
                    var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Script Assemblies");
    
                    if (Directory.Exists(path) == false)
                    {
                        Directory.CreateDirectory(path);
                    }
                    return path;
    
                }
            }
    
            static void Main(string[] args)
            {
                var issues = CompileSourceCodeDom(code);
    
                if (issues.Errors.Count == 0)
                    ExecuteFromAssembly(issues.CompiledAssembly);
            }
    
            private static string code = @"class InternalRunner
    {
            public void SomeMethod() { }
    
            private void SomeMethod2() { }
    }";
            private static void ExecuteFromAssembly(Assembly compiledAssembly)
            {
                Type fooType = compiledAssembly.GetType("InternalRunner");
                MethodInfo printMethod2 = fooType.GetMethod("SomeMethod2");
                object foo = compiledAssembly.CreateInstance("InternalRunner");
                printMethod2.Invoke(foo, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static, null, null, CultureInfo.CurrentCulture);
    
            }
    
            private static CompilerResults CompileSourceCodeDom(string sourceCode)
            {
    
                CodeDomProvider cpd = new CSharpCodeProvider();
                CompilerParameters cp = new CompilerParameters();
                var assemblies = AppDomain.CurrentDomain
                    .GetAssemblies()
                    .Where(a => !a.IsDynamic)
                    .Select(a => a.Location);
    
                cp.ReferencedAssemblies.AddRange(assemblies.ToArray());
    
                cp.GenerateInMemory = false;
                cp.GenerateExecutable = false;
                
                String assmlocation = Path.Combine(AssembliesPath, Guid.NewGuid() + ".dll");
                cp.OutputAssembly = assmlocation;
    
                CompilerResults cr = cpd.CompileAssemblyFromSource(cp, sourceCode);
    
                cpd.Dispose();
    
                return cr;
            }
        }
    }

    Regards,

    loftty


    • Edited by loftty Thursday, May 23, 2019 8:32 AM
    Thursday, May 23, 2019 8:15 AM

Answers

  • Hi 

    Thanks for the feedback.

    You could try the following change to call a private method.

    Change:

     MethodInfo printMethod2 = fooType.GetMethod("SomeMethod2");

    Into

       MethodInfo printMethod2 = fooType.GetMethod("SomeMethod2",BindingFlags.NonPublic|BindingFlags.Instance);

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by loftty Thursday, May 23, 2019 10:34 AM
    Thursday, May 23, 2019 9:47 AM
    Moderator

All replies

  • Hi loftty,

    Thank you for posting here.

    According to your description, you want to know why it returns a null when you try to invoke the printmethod2. 

    Your thought is correct. An object that represents the public method with the specified name, if found; otherwise, null. The sentence comes from the following link, you could have a look. 

    https://docs.microsoft.com/en-us/dotnet/api/system.type.getmethod?view=netframework-4.8#System_Type_GetMethod_System_String_​​​​​​​

    Therefore, when someMethod2 is private, printMethod2 will return null.

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, May 23, 2019 9:02 AM
    Moderator
  • Hi Jack,

    That is my problem, how do I call a private method?

    Regards,

    Loftty

    Thursday, May 23, 2019 9:35 AM
  • Hi 

    Thanks for the feedback.

    You could try the following change to call a private method.

    Change:

     MethodInfo printMethod2 = fooType.GetMethod("SomeMethod2");

    Into

       MethodInfo printMethod2 = fooType.GetMethod("SomeMethod2",BindingFlags.NonPublic|BindingFlags.Instance);

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by loftty Thursday, May 23, 2019 10:34 AM
    Thursday, May 23, 2019 9:47 AM
    Moderator
  • Hi Jack,

    I cannot believe that is all I needed :)

    Thanks for your help.

    Regards,

    Loftty

    Thursday, May 23, 2019 10:35 AM