本文整理匯總了C#中System.Delegate.DynamicInvoke方法的典型用法代碼示例。如果您正苦於以下問題:C# Delegate.DynamicInvoke方法的具體用法?C# Delegate.DynamicInvoke怎麽用?C# Delegate.DynamicInvoke使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Delegate
的用法示例。
在下文中一共展示了Delegate.DynamicInvoke方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: DelayedInvoke
/// <summary>
/// Initializes a new instance of the <see cref="DelayedInvoke"/> class.
/// </summary>
/// <param name="action">The action to perform.</param>
/// <param name="timeout">The timeout before running.</param>
/// <param name="blocking">if set to <c>true</c> then the delay will block.</param>
/// <param name="args">The arguments to pass to the action.</param>
public DelayedInvoke(Delegate action, int timeout, bool blocking, object[] args = null)
{
if (timeout == 0)
{
action.DynamicInvoke(args);
}
if (blocking)
{
do
{
Restart = false;
Thread.Sleep(timeout);
} while (Restart);
HasReturned = true;
ReturnValue = action.DynamicInvoke(args);
}
else
{
thread = new Thread(delegate()
{
do
{
Restart = false;
Thread.Sleep(timeout);
} while (Restart);
HasReturned = true;
ReturnValue = action.DynamicInvoke(args);
});
thread.Name = "DelayedInvoke: " + action.Method.Name;
thread.Start();
}
}
示例2: makeMethodCall
public void makeMethodCall(Delegate methodToCall)
{
addArgsTextBox.Text = "Click new constructor or method.";
object[] argObjects = new object[methodArgs.Count];
if (argObjects.Length != methodToCall.Method.GetParameters().Length)
{
System.Windows.Forms.MessageBox.Show("Not enough arguments");
methodArgs.Clear();
enteredArgsLabel.Text = "Entered Args";
return;
}
string stringType = "";
int i = 0;
ParameterInfo[] instancePars = methodToCall.Method.GetParameters();
foreach (object ob in methodArgs)
{
if (ob != null)
{
stringType = instancePars[i].ParameterType.Name;
try
{
argObjects[i] = parseObject(stringType, ob);
}
catch (Exception Exception)
{
System.Windows.Forms.MessageBox.Show("err: " + Exception.Message);
clearArgsButt_Click(null, null);
return;
}
i++;
}
}
object[] returned = null;
object returned_Temp = null;
if (argObjects.Length == 0)
{
returned_Temp = methodToCall.DynamicInvoke();
}
else
{
returned_Temp = methodToCall.DynamicInvoke(argObjects);
}
if (methodToCall.Method.ReturnParameter.ParameterType != typeof(void))
{
returned = new object[1];
returned[0] = returned_Temp;
}
if (returned != null)
{
objectsListBox.Items.Add(returned[0]);
}
enteredArgsLabel.Text = "Entered Args";
methodArgs.Clear();
}
示例3: BeginInvoke
/// <summary>
/// Forwards the BeginInvoke to the current application's <see cref="Dispatcher"/>.
/// </summary>
/// <param name="method">Method to be invoked.</param>
/// <param name="arg">Arguments to pass to the invoked method.</param>
public Task BeginInvoke(Delegate method, params object[] arg)
{
if (dispatcher != null)
return dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => method.DynamicInvoke(arg)).AsTask();
var coreWindow = CoreApplication.MainView.CoreWindow;
if (coreWindow != null)
{
dispatcher = coreWindow.Dispatcher;
return dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => method.DynamicInvoke(arg)).AsTask();
}
else
return Task.Delay(0);
}
示例4: InvokeEvent
/// <summary>
/// Invokes a .NET event
/// </summary>
/// <param name="handler">Event to be invoked</param>
/// <param name="args">Paramters to pass to the event</param>
public void InvokeEvent(Delegate handler, params object[] args)
{
if (handler != null)
{
handler.DynamicInvoke(args);
}
}
示例5: Core_RunInGui
public void Core_RunInGui(Delegate method, params object[] args)
{
if (method == null)
return;
RunOnUiThread(new Action(() => method.DynamicInvoke(args)));
}
示例6: DoInvoke
private object DoInvoke(Guid relatedActivityId, Delegate method, object[] methodArgs)
{
using (_eventCorrelator.StartActivity(relatedActivityId))
{
return method.DynamicInvoke(methodArgs);
}
}
示例7: Invoke
public object Invoke(Delegate method, params object[] args)
{
object result = null;
Exception exception = null;
using (ManualResetEvent waitHandle = new ManualResetEvent(false))
{
_queue._queue.Enqueue(
delegate()
{
try
{
result = method.DynamicInvoke(args);
}
catch (Exception e)
{
exception = e;
}
waitHandle.Set();
});
waitHandle.WaitOne();
}
if (exception != null)
{
throw new ReactorInvocationException("An exception occurred invoking a method " +
"in a reactor thread; check InnerException for the exception.", exception);
}
else
{
return result;
}
}
示例8: BeginInvoke
/// <summary>
/// Forwards the BeginInvoke to the current application's <see cref="Dispatcher"/>.
/// </summary>
/// <param name="method">Method to be invoked.</param>
/// <param name="arg">Arguments to pass to the invoked method.</param>
public void BeginInvoke(Delegate method, object arg)
{
if (Application.Current != null)
{
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, method, arg);
}
else if (SynchronizationContext.Current != null)
{
SendOrPostCallback callback = new SendOrPostCallback(delegate { method.DynamicInvoke(arg); });
SynchronizationContext.Current.Post(callback, null);
}
else
{
method.DynamicInvoke(arg);
}
}
示例9: Execute
/// <summary>Invokes the specified delegate with the specified parameters in the specified kind of apartment state.</summary>
/// <param name="d">The delegate to be invoked.</param>
/// <param name="parameters">The parameters to pass to the delegate being invoked.</param>
/// <param name="state">The apartment state to run under.</param>
/// <returns>The result of calling the delegate.</returns>
public static object Execute(
Delegate d, object[] parameters, ApartmentState state)
{
if (d == null) throw new ArgumentNullException("d");
if (state != ApartmentState.MTA && state != ApartmentState.STA)
throw new ArgumentOutOfRangeException("state");
if (Thread.CurrentThread.ApartmentState == state)
{
return d.DynamicInvoke(parameters);
}
else
{
ApartmentStateSwitcher switcher = new ApartmentStateSwitcher();
switcher._delegate = d;
switcher._parameters = parameters;
Thread t = new Thread(new ThreadStart(switcher.Run));
t.ApartmentState = state;
t.IsBackground = true;
t.Start();
t.Join();
if (switcher._exc != null) throw switcher._exc;
return switcher._rv;
}
}
示例10: ExecuteDelegate
/// <summary>
/// Executes the specified delegate on the UI thread
/// </summary>
/// <param name="del"> </param>
/// <param name="arguments"> </param>
public static void ExecuteDelegate(Delegate del, params object[] arguments) {
if (CheckAccess()) {
del.DynamicInvoke(arguments);
} else {
UserInterfaceDispatcher.BeginInvoke(del, arguments);
}
}
示例11: Cache
public static object Cache(string key, int cacheTimeMilliseconds, Delegate result, params object[] args)
{
var cache = MemoryCache.Default;
if (cache[key] == null)
{
object cacheValue;
try
{
if (result == null)
return null;
cacheValue = result.DynamicInvoke(args);
}
catch (Exception e)
{
var message = e.InnerException.GetBaseException().Message;
throw new CacheDelegateMethodException(message);
}
if (cacheValue != null)
{
cache.Set(key, cacheValue, DateTimeOffset.Now.AddMilliseconds(cacheTimeMilliseconds));
}
}
return cache[key];
}
示例12: Delete
public static string Delete(int id, Delegate del, string ItemType)
{
var message = string.Empty;
var sqlError = string.Empty;
var errorMessage = string.Format("Error deleting {0} item (id={1}).", ItemType, id);
try
{
var r = del.DynamicInvoke(id, sqlError);
//Return actual error message, if any
if (!string.IsNullOrEmpty(sqlError))
throw new Exception(errorMessage += " " + sqlError);
else if ((int)r != 0)
throw new Exception(string.Format("{0} {1} returned {2}.", errorMessage, del.Method.Name, r));
}
catch (Exception exc)
{
message = errorMessage;
Global.logger.ErrorException(message, exc);
throw;
}
return message;
}
示例13: Call5
public string Call5(Delegate callback)
{
var thisArg = JsValue.Undefined;
var arguments = new JsValue[] { 1, "foo" };
return callback.DynamicInvoke(thisArg, arguments).ToString();
}
示例14: Invoke
public object Invoke(Delegate method, object[] args)
{
//Uses NSObject.InvokeOnMainThread
object result = null;
InvokeOnMainThread (() => result = method.DynamicInvoke (args));
return result;
}
示例15: InvokeUntypedDelegate
public static void InvokeUntypedDelegate(Delegate d, params object[] args) {
#if SILVERLIGHT
IronPython.Runtime.Operations.PythonCalls.Call(d, args);
#else
d.DynamicInvoke(args);
#endif
}