class Device{
private object device;
public Device(string ProgID)
{
if (ProgID == "") ProgID = "ScopeSim.Telescope";
device = Activator.CreateInstance(Type.GetTypeFromProgID(ProgID));
Console.WriteLine("Connected");
}
public object Invoke(string Name, object[] args)
{
var v1 = device.GetType(); //this is a com object in debug
var v2 = v1.GetMethod(Name);
var v3 = v2.Invoke(device,args); //throws exception, v2 is null
return v3;
}
}
//somwhere else in another method in another class that has this in a field...
Console.WriteLine(new Device("").Invoke("A Method Name that is a string but is not known and could be anything, for testing, the name is 'Unpark'", object[] args));
This throws a NullReferenceException
. The Unpark method does exist but it does not have a return type, but it does exist. Also, when it stopped to debug (on the exception) the ProgID field in the constructor was null. I would assume that this is normal though, right? It would have already run. Does anyone know why it throws it? If I declare device as dynamic
, says it can't bind at runtime to a null object (basically the same thing).
Response to First Answer: I think reflection requires the variables as an array of objects. Yes, Unpark is written with a capital U. The ProgID thing apparently seems to be irrelevant.