本文整理汇总了C#中Delegate.DynamicInvoke方法的典型用法代码示例。如果您正苦于以下问题:C# Delegate.DynamicInvoke方法的具体用法?C# Delegate.DynamicInvoke怎么用?C# Delegate.DynamicInvoke使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Delegate
的用法示例。
在下文中一共展示了Delegate.DynamicInvoke方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Invoke
public object Invoke(Delegate method, object[] args) {
if (InvokeRequired)
{
var asyncResult = BeginInvoke(method, args);
return EndInvoke(asyncResult);
}
else
{
return method.DynamicInvoke(args);
}
}
示例2: InternalRealCall
private object InternalRealCall(Delegate callback, object args, int numArgs)
{
object result = null;
Debug.Assert(numArgs == 0 || // old API, no args
numArgs == 1 || // old API, 1 arg, the args param is it
numArgs == -1); // new API, any number of args, the args param is an array of them
// Support the fast-path for certain 0-param and 1-param delegates, even
// of an arbitrary "params object[]" is passed.
int numArgsEx = numArgs;
object singleArg = args;
if(numArgs == -1)
{
object[] argsArr = (object[])args;
if (argsArr == null || argsArr.Length == 0)
{
numArgsEx = 0;
}
else if(argsArr.Length == 1)
{
numArgsEx = 1;
singleArg = argsArr[0];
}
}
// Special-case delegates that we know about to avoid the
// expensive DynamicInvoke call.
if(numArgsEx == 0)
{
Action action = callback as Action;
if (action != null)
{
action();
}
else
{
Dispatcher.ShutdownCallback shutdownCallback = callback as Dispatcher.ShutdownCallback;
if(shutdownCallback != null)
{
shutdownCallback();
}
else
{
// The delegate could return anything.
result = callback.DynamicInvoke();
}
}
}
else if(numArgsEx == 1)
{
DispatcherOperationCallback dispatcherOperationCallback = callback as DispatcherOperationCallback;
if(dispatcherOperationCallback != null)
{
result = dispatcherOperationCallback(singleArg);
}
else
{
SendOrPostCallback sendOrPostCallback = callback as SendOrPostCallback;
if(sendOrPostCallback != null)
{
sendOrPostCallback(singleArg);
}
else
{
if(numArgs == -1)
{
// Explicitly pass an object[] to DynamicInvoke so that
// it will not try to wrap the arg in another object[].
result = callback.DynamicInvoke((object[])args);
}
else
{
// By pass the args parameter as a single object,
// DynamicInvoke will wrap it in an object[] due to the
// params keyword.
result = callback.DynamicInvoke(args);
}
}
}
}
else
{
// Explicitly pass an object[] to DynamicInvoke so that
// it will not try to wrap the arg in another object[].
result = callback.DynamicInvoke((object[])args);
}
return result;
}
示例3: CatchException
// This returns false when caller should rethrow the exception.
// true means Exception is "handled" and things just continue on.
private bool CatchException(object source, Exception e, Delegate catchHandler)
{
if (catchHandler != null)
{
if(catchHandler is DispatcherOperationCallback)
{
((DispatcherOperationCallback)catchHandler)(null);
}
else
{
catchHandler.DynamicInvoke(null);
}
}
if(null != Catch)
return Catch(source, e);
return false;
}
示例4: Invoke
private static void Invoke( Delegate evt, EventArgs args ) {
if ( evt != null ) {
evt.DynamicInvoke( null, args );
}
}
示例5: BeginInvoke
/// <summary>
/// Invoke in UI thread.
/// </summary>
public void BeginInvoke(Delegate d, params object[] args)
{
var h = new Handler(Looper.MainLooper);
h.Post(() => d.DynamicInvoke(args));
}
示例6: InvokeWrappedDelegate
/// <summary>
/// Invokes the wrapped delegate synchronously
/// </summary>
/// <param name="d"></param>
/// <param name="args"></param>
static void InvokeWrappedDelegate(Delegate d, object[] args)
{
d.DynamicInvoke(args);
}
示例7: InvokeInternal
private static object InvokeInternal(Delegate d, object[] args)
{
return d.DynamicInvoke(args);
}
示例8: BeginInvoke
/// <summary>
/// Invoke in UI thread.
/// </summary>
public void BeginInvoke(Delegate d, params object[] args)
{
d.DynamicInvoke(args);
}
示例9: Invoke
/// <summary>
/// Executes the specified delegate with the specified arguments synchronously on the UI thread.
/// <para /> If current thread is the UI thread, the delegate will be executed immediately, otherwise execution will be queued on the UI thread.
/// </summary>
/// <param name="d">Delegate to execute.</param>
/// <param name="args">Arguments to pass to the delegate.</param>
/// <returns>The value returned from the delegate or null if the delegate has no return value.</returns>
public object Invoke(Delegate d, params object[] args)
{
if (CheckAccess())
{
return d.DynamicInvoke(args);
}
else
{
return InvokeInternal(d, args);
}
}
示例10: Parse
// --------------------------------------------------
/// <summary>
/// Parses the dynamic lambda expression given in the form of a delegate, and creates and returns a new instance of
/// <see cref="DynamicParser"/> to maintain the results.
/// </summary>
/// <param name="func">The dynamic lambda expression or delegate to parser.</param>
/// <returns>The instance holding the results of the parsing.</returns>
static public DynamicParser Parse( Delegate func )
{
DEBUG.IndentLine( "\n-- DynamicParser.Parse( Func )" );
if( func == null ) throw new ArgumentNullException( "func", "Delegate to parse cannot be null." );
DynamicParser parser = new DynamicParser();
ParameterInfo[] pars = func.Method.GetParameters(); foreach( var par in pars ) {
bool isdynamic = par.GetCustomAttributes( typeof( DynamicAttribute ), true ).Length != 0 ? true : false;
if( isdynamic ) {
var dyn = new DynamicNode.Argument( par.Name ) { _Parser = parser };
parser._Arguments.Add( dyn );
}
else throw new ArgumentException( string.Format( "Argument '{0}' is not dynamic.", par.Name ) );
}
parser._Returned = func.DynamicInvoke( parser._Arguments.ToArray() );
DEBUG.UnindentLine( "\n-- Parsed={0}", parser );
return parser;
}
示例11: CacheLastN
/// <summary>
/// Caches the last N objects based on the time (last accessed time)
/// </summary>
/// <param name="lastN">Number of objects to cache</param>
/// <param name="time">The time in minutes</param>
/// <param name="method">The method getting the object</param>
/// <param name="methodArgs">Parameters of the method (they are also used to identify the objects and generate the cache key)</param>
/// <returns>Cached or retrieved object</returns>
static object CacheLastN(int lastN, int time, Delegate method, params object[] methodArgs)
{
// now time
DateTime nowTime = DateTime.Now;
//Generating an unique cache item name for the cached data
string safeCacheItemName = "last_n_";
foreach (object argument in methodArgs)
{
// cache keys are always lower case
safeCacheItemName += ValidationHelper.GetCodeName(argument).Replace('.', '_').ToLower() + "_";
}
safeCacheItemName = safeCacheItemName.TrimEnd('_');
// if the cache item is available, update the date time stamp
if (LastCacheItemNames.ContainsKey(safeCacheItemName))
{
LastCacheItemNames[safeCacheItemName] = nowTime;
}
else
{
// remove the oldest item if we have already N items
if (LastCacheItemNames.Count >= lastN)
{
string removedItemKey = LastCacheItemNames.OrderBy(key => key.Value).First().Key;
LastCacheItemNames.Remove(removedItemKey);
//remove it from the cache
CacheHelper.TouchKey(removedItemKey);
}
// add the new item
LastCacheItemNames.Add(safeCacheItemName, nowTime);
}
// let our cache method handle the retrieval and creation of the cache
return CacheHelper.Cache(cs => method.DynamicInvoke(methodArgs), new CacheSettings(time, safeCacheItemName));
}