mercredi 7 décembre 2016

Invoking Method by name using Reflection is not working in c#

I am trying to invoke a method from another class but it's not working Here is my code...

namespace HealthClub
{
    public partial class frmTrainerMaster : Form
    {
         public void MyMethod()
         {
                   // Some Code
         }
    }
}

Now in Another class

public void TestMethod(string ClassName, string MethodName)
{
     Type tp = Type.GetType("HealthClub." + ClassName);  // HealthClub is NameSpace
     object myobj = Activator.CreateInstance(tp, null);
     MethodInfo method = myobj.GetType().GetMethod(MethodName);
     method.Invoke(mybj, null);
}

But it's through an exception "Object reference not set to instance of an object".

What I am doing wrong here ?





Aucun commentaire:

Enregistrer un commentaire