Invokemember method cannot call method under a public static class

882 Views Asked by At

So I'm calling a method from my dll using this.

string sp = "dynamicmethodname";

Type TypeObj = typeof(DLLclass);
Object MyObj = Activator.CreateInstance(TypeObj);
TypeObj.InvokeMember(sp, BindingFlags.InvokeMethod | BindingFlags.Default, null, MyObj, new Object[] { gp });  

it will work if my method is just under my public class. But when I tried to do something like this.

public class Test {
   public static class Met1{
     public static void _Validate(string gp){
      functions here.....
    }
  }
}

The invokemember method won't reach my _Validate method anymore. I wonder why it won't work anymore.

1

There are 1 best solutions below

0
On

Full example of what @Charleh wrote:

public class Test
{
    public static class Met1
    {
        public static void _Validate(string gp)
        {
            Console.WriteLine(gp);
        }
    }
}

MethodInfo method = typeof(Test.Met1).GetMethod("_Validate");
// Or 
// MethodInfo method = typeof(Test.Met1).GetMethod("_Validate", BindingFlags.Static | BindingFlags.Public);
// or
// MethodInfo method = typeof(Test.Met1).GetMethod("_Validate", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(string) }, null);

method.Invoke(null, new object[] { "Hello world " });

Compiled: https://ideone.com/JIoHar