Reputation: 73
I want to call method from another assembly in WinForm like this
private void loadToolStripMenuItem_Click(object sender, EventArgs e){
Thread thread = new Thread(() =>
{
string assemblyPath = "PluginForSnake.dll";
AppDomain domain = AppDomain.CreateDomain("MyNewDomain");
ObjectHandle handle = domain.CreateInstanceFrom(assemblyPath, "PluginForSnake.Save");
object obj = handle.Unwrap();
MessageBox.Show("found");
if (RemotingServices.IsTransparentProxy(obj)){
Type type = obj.GetType();
object[] param = new object[] { _snake, _food };
MethodInfo saveGame = type.GetMethod("saveGame");
saveGame.Invoke(obj, param);
}
});
thread.IsBackground = true;
thread.Start();
}
But im getting this exception at Invoke line
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
Upvotes: 0
Views: 654
Reputation: 116
TargetInvocationException can be hell to figure out. There are a few ways to make it easier for you.
First, you can try to use Task
, for better exception handling [1]
And/or, you can create a delegate[2] and call it as you would a function, instead of Invoking it. This will give you whatever exception is thrown by the saveGame
function instead of "the invoke failed". The process of creating the delegate can also reveal other issues with the function you are trying to Invoke, and is a great learning excercise.
[1] catch exception that is thrown in different thread
[2] https://msdn.microsoft.com/en-us/library/ms173176.aspx
Upvotes: 1