jeudi 11 novembre 2021

How to call a private method using reflection

I am trying to call a private method using reflection. This is what I have tried:

private void CallMethod(string MethodName, object[] Arguments)
{
   System.Reflection.Assembly.GetExecutingAssembly().GetTypes().First(x => x.Name == "Form1").GetMethod(MethodName, System.Reflection.BindingFlags.NonPublic).Invoke(null, Arguments);
}

private void Method1()
{
   //Code...
}

And when I call CallMethod like this:

CallMethod("Method1", null);

It throws a NullReferenceException. I can a solve this problem? Thanks for any help.





Aucun commentaire:

Enregistrer un commentaire