invoke

Invoking methods with optional parameters through reflection

ε祈祈猫儿з 提交于 2019-11-26 20:25:57
I've run into another problem using C# 4.0 with optional parameters. How do I invoke a function (or rather a constructor, I have the ConstructorInfo object) for which I know it doesn't require any parameters? Here is the code I use now: type.GetParameterlessConstructor() .Invoke(BindingFlags.OptionalParamBinding | BindingFlags.InvokeMethod | BindingFlags.CreateInstance, null, new object[0], CultureInfo.InvariantCulture); (I've just tried with different BindingFlags ). GetParameterlessConstructor is a custom extension method I wrote for Type . According to MSDN , to use the default parameter

Javascript dynamically invoke object method from string

廉价感情. 提交于 2019-11-26 17:29:27
Can I dynamically call an object method having the method name as a string? I would imagine it like this: var FooClass = function() { this.smile = function() {}; } var method = "smile"; var foo = new FooClass(); // I want to run smile on the foo instance. foo.{mysterious code}(); // being executed as foo.smile(); if the name of the property is stored in a variable, use [] foo[method](); Properties of objects can be accessed through the array notation: var method = "smile"; foo[method](); // will execute the method "smile" method can be call with eval eval("foo." + method + "()"); might not be

Is it appropriate to extend Control to provide consistently safe Invoke/BeginInvoke functionality?

半腔热情 提交于 2019-11-26 17:26:41
In the course of my maintenance for an older application that badly violated the cross-thread update rules in winforms, I created the following extension method as a way to quickly fix illegal calls when I've discovered them: /// <summary> /// Execute a method on the control's owning thread. /// </summary> /// <param name="uiElement">The control that is being updated.</param> /// <param name="updater">The method that updates uiElement.</param> /// <param name="forceSynchronous">True to force synchronous execution of /// updater. False to allow asynchronous execution if the call is marshalled /

Best Way to Invoke Any Cross-Threaded Code?

Deadly 提交于 2019-11-26 15:43:17
I know that this question has been asked before, but I'm looking for a way to: streamline the creation of safe cross-threaded code. reuse this code in any situation (no Windows Forms references). Here's what I have so far, but I want to remove the Windows Forms references. Any ideas? public delegate void SafeInvokeDelegate(System.Action action); public class SafeInvoke { private readonly System.Windows.Forms.Control _threadControl; public SafeInvoke() { _threadControl = new System.Windows.Forms.Control(); } public void Invoke(System.Action action) { if (_threadControl.InvokeRequired)

Ensuring that things run on the UI thread in WPF

喜夏-厌秋 提交于 2019-11-26 15:32:20
问题 I'm building a WPF application. I'm doing some async communication with the server side, and I use event aggregation with Prism on the client. Both these things results in new threads to be spawned which are not the UI thread. If I attempt to do "WPF operations" on these callback and event handler threads the world will fall apart, which it now has started doing. First I met problems trying to create some WPF objects in the callback from server. I was told that the thread needed to run in STA

How to call a method stored in a HashMap? (Java) [duplicate]

社会主义新天地 提交于 2019-11-26 15:01:48
This question already has an answer here: Function pointers/delegates in Java? 9 answers I have a list of commands (i, h, t, etc) that the user will be entering on a command line/terminal Java program. I would like to store a hash of command/method pairs: 'h', showHelp() 't', teleport() So that I can have code something like: HashMap cmdList = new HashMap(); cmdList.put('h', showHelp()); if(!cmdList.containsKey('h')) System.out.print("No such command.") else cmdList.getValue('h') // This should run showHelp(). Is this possible? If not, what is an easy way to this? With Java 8+ and Lambda

Invoke in Windows Forms

情到浓时终转凉″ 提交于 2019-11-26 14:49:13
问题 Does anyone have a link to a learning resource for using Invoke? I'm trying to learn but all the examples I have seen I have been unable to adapt for my purposes. 回答1: Did you try MSDN Control.Invoke I just wrote a little WinForm application to demonstrate Control.Invoke. When the form is created, Start some work on background thread. After that work is done, Update the status in a label. public Form1() { InitializeComponent(); //Do some work on a new thread Thread backgroundThread = new

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

 ̄綄美尐妖づ 提交于 2019-11-26 14:36:57
问题 I have a call to MethodInfo.Invoke() to execute a function through reflection. The call is wrapped in a try/catch block but it still won't catch the exception thrown by the function I'm invoking. I receive the following message: Exception was unhandled by the user. Why does MethodInfo.Invoke() prevent the Exception to be caught outside of the Invoke() ? How do I bypass it? 回答1: EDIT: As I understand your issue, the problem is purely an IDE one; you don't like VS treating the exception thrown

Avoid calling Invoke when the control is disposed

五迷三道 提交于 2019-11-26 14:19:20
问题 I have the following code in my worker thread ( ImageListView below is derived from Control ): if (mImageListView != null && mImageListView.IsHandleCreated && !mImageListView.IsDisposed) { if (mImageListView.InvokeRequired) mImageListView.Invoke( new RefreshDelegateInternal(mImageListView.RefreshInternal)); else mImageListView.RefreshInternal(); } However, I get an ObjectDisposedException sometimes with the Invoke method above. It appears that the control can be disposed between the time I

Dispatcher Invoke(…) vs BeginInvoke(…) confusion

假装没事ソ 提交于 2019-11-26 12:58:07
问题 I\'m confused why I can\'t make this test counter application work with 2 (or more) simultaneous running countertextboxes with the use of \"BeginInvoke\" on my Dispatcher in the Count() method. You can solve the issue by replacing the BeginInvoke by an Invoke. But this doesn\'t solve my confusion. Here\'s the sample code I\'m talking about: public class CounterTextBox : TextBox { private int _number; public void Start() { (new Action(Count)).BeginInvoke(null, null); } private void Count() {