Friday, 6 September 2013

Assembly InvokeMember try{} catch()

Assembly InvokeMember try{} catch()

we're currently working on a program which has an open C# API interface.
Now, imagine you have the following C# script:
int[] Test = { 0, 3 };
private void Test()
{
Debug.Log(Test[4].ToString()); //Exception!!
}
Compiling works, however, if we now try to Invoke the Test Void it will
fail. It should obviously fail, but we want to catch the Exception, so our
whole Application doesn't freeze.
Assembly assembly = Assembly.LoadFrom("C:/Our/Path/To/Dll.dll");
Type type = assembly.GetType("ClassApplication1.Test");
object instance = Activator.CreateInstance(type);
type.InvokeMember("Test",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic
| BindingFlags.IgnoreReturn,
null,
instance,
null);
This popup will appear in Visual Studio 2012 if we're debugging, without
debugging our Application simply crashes completly.

No comments:

Post a Comment