Reflection MethodInfo.Invoke() catch exceptions from inside the method

EDIT: As I understand your issue, the problem is purely an IDE one; you don’t like VS treating the exception thrown by the invocation of the MethodInfo as uncaught, when it clearly isn’t. You can read about how to resolve this problem here: Why is TargetInvocationException treated as uncaught by the IDE? It appears to … Read more

Communication between BroadcastReceiver and Activity – android

You can use observers , like public class MyReceiver extends BroadcastReceiver { public MyReceiver() { } @Override public void onReceive(Context context, Intent intent) { ObservableObject.getInstance().updateValue(intent); } } public class MainActivity extends Activity implements Observer { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ObservableObject.getInstance().addObserver(this); } @Override public void update(Observable observable, Object data) { Toast.makeText(this, String.valueOf(“activity observer … Read more

Dispatcher Invoke(…) vs BeginInvoke(…) confusion

When you use Dispatcher.BeginInvoke it means that it schedules the given action for execution in the UI thread at a later point in time, and then returns control to allow the current thread to continue executing. Invoke blocks the caller until the scheduled action finishes. When you use BeginInvoke your loop is going to run … Read more

How to use Reflection to Invoke an Overloaded Method in .NET

You have to specify which method you want: class SomeType { void Foo(int size, string bar) { } void Foo() { } } SomeType obj = new SomeType(); // call with int and string arguments obj.GetType() .GetMethod(“Foo”, new Type[] { typeof(int), typeof(string) }) .Invoke(obj, new object[] { 42, “Hello” }); // call without arguments obj.GetType() … Read more

C# DllImport with C++ boolean function not returning correctly

I found the solution for your problem. Your declaration should be preceded with this marshaling: [return:MarshalAs(UnmanagedType.I1)] so everything should look like this: [DllImport(“Whisper.dll”, EntryPoint=”Exist”, CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool Exist([MarshalAs(UnmanagedType.LPStr)] string name); I tested it in my very simple example and it worked! EDIT Why this happens? C defines bool as 4 bytes int … Read more

Invoking methods with optional parameters through reflection

According to MSDN, to use the default parameter you should pass Type.Missing. If your constructor has three optional arguments then instead of passing an empty object array you’d pass a three element object array where each element’s value is Type.Missing, e.g. type.GetParameterlessConstructor() .Invoke(BindingFlags.OptionalParamBinding | BindingFlags.InvokeMethod | BindingFlags.CreateInstance, null, new object[] { Type.Missing, Type.Missing, Type.Missing }, … Read more

How to call a function by its name (std::string) in C++?

What you have described is called reflection and C++ doesn’t support it. However you might come with some work-around, for example in this very concrete case you might use an std::map that would map names of functions (std::string objects) to function pointers, which in case of functions with the very same prototype could be easier … Read more

Best Way to Invoke Any Cross-Threaded Code?

You also could use an extension method and lambdas to make your code much cleaner. using System.ComponentModel; public static class ISynchronizeInvokeExtensions { public static void InvokeEx<T>(this T @this, Action<T> action) where T : ISynchronizeInvoke { if (@this.InvokeRequired) { @this.Invoke(action, new object[] { @this }); } else { action(@this); } } } So now you can … Read more