The SportsCar class derives from the base class Car which contains an enum. I am trying to use refelection to access the TurnOnRadio method in the SportsCar class which uses the enum. The value 2 in the line mi.Invoke(obj, new object[] { true, 2 }); is for the enum. When I run the code, I receive an error : Object reference not set to an instance of an object.
Any assistance would be greatly appreciated. Thank you :
static void InvokeMethodWithArgsUsingLateBinding(Assembly asm) { try { // Firstly, get a metadata description of the sports car : Type sport = asm.GetType("CarLibrary2.SportsCar");
// Now create the sports car :
object obj = Activator.CreateInstance(sport);
// Invoke the TurnOnRadio() method which takes 2 arguments :
MethodInfo mi = sport.GetMethod("TurnOnRadio");
mi.Invoke(obj, new object[] { true, 2 } );
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Aucun commentaire:
Enregistrer un commentaire