本文整理汇总了C#中System.Threading.Tasks.Task.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# Task.GetType方法的具体用法?C# Task.GetType怎么用?C# Task.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.Tasks.Task
的用法示例。
在下文中一共展示了Task.GetType方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HandleCompleted
protected IObservable<object> HandleCompleted(Task task)
{
var resultProperty = task.GetType().GetTypeInfo().GetDeclaredProperty("Result");
if (resultProperty != null)
{
switch (task.Status)
{
case TaskStatus.RanToCompletion:
return Observable.Return(resultProperty.GetValue(task));
case TaskStatus.Faulted:
return Observable.Return(new BindingNotification(task.Exception, BindingErrorType.Error));
default:
throw new AvaloniaInternalException("HandleCompleted called for non-completed Task.");
}
}
return Observable.Empty<object>();
}
示例2: GetTaskInfo
private static Tuple<object, Type> GetTaskInfo(Task task)
{
Type taskType = task.GetType();
bool isTypedTask = taskType.IsGenericTask();
bool isVoidTask = !isTypedTask && typeof(Task).IsAssignableFrom(taskType);
if (!isTypedTask && !isVoidTask)
{
throw new InvalidOperationException(Resources.Global.InvalidIAsyncResultReturned);
}
if (isVoidTask)
{
return new Tuple<object, Type>(null, typeof(void));
}
Type taskResultType = taskType.GetGenericArguments()[0];
var dynamicTask = (dynamic) task;
return new Tuple<object, Type>(dynamicTask.Result, taskResultType);
}
示例3: GetResult
public static object GetResult(Task task) => task.GetType() == typeof(Task) ? null : ((dynamic)task).Result;
示例4: GetTaskResult
private object GetTaskResult(Task task)
{
return task.GetType().GetProperty("Result").GetValue(task);
}
示例5: GetTaskResult
/// <summary>
/// get result of task
/// </summary>
/// <param name="task"></param>
/// <returns></returns>
private static object GetTaskResult(Task task)
{
try
{
var type = task.GetType();
var func = GetTaskResultFuncs.GetOrAdd(type, (Type key) =>
{
var arg = Expression.Parameter(typeof(object), "task");
var argConvert = Expression.Convert(arg, type);
var expProp = Expression.Property(argConvert, "Result");
var expConvert = Expression.Convert(expProp, typeof(object));
var asignExp = Expression.Lambda(expConvert, arg);
var getFunc = asignExp.Compile();
return (Func<object, object>)getFunc;
});
var value = func?.Invoke(task);
return value;
}
catch (Exception ex)
{
LogHelper.Error("GetTaskResult error", ex);
var aex = ex as AggregateException;
if (aex?.InnerException != null)
throw aex.InnerException;
throw;
}
}
示例6: InvokeAsync_IfLambdaReturnsNonGenericTask_ReturnsCompletedTask
public void InvokeAsync_IfLambdaReturnsNonGenericTask_ReturnsCompletedTask()
{
// Arrange
Func<object, object[], Task> lambda = (i1, i2) =>
{
Task innerTask = new Task(() => { });
innerTask.Start();
Assert.False(innerTask.GetType().IsGenericType); // Guard
return innerTask;
};
IMethodInvoker<object> invoker = CreateProductUnderTest(lambda);
object instance = null;
object[] arguments = null;
// Act
Task task = invoker.InvokeAsync(instance, arguments);
// Assert
Assert.NotNull(task);
task.WaitUntilCompleted();
Assert.Equal(TaskStatus.RanToCompletion, task.Status);
}
示例7: GetTaskResult
private static object GetTaskResult(Task task)
{
var taskType = task.GetType();
var typeInfo = taskType.GetTypeInfo();
if (!typeInfo.IsGenericType) {
throw new SomehowRecievedTaskWithoutResultException();
}
var resultProperty = typeInfo.GetDeclaredProperty("Result").GetMethod;
return resultProperty.Invoke(task, new object[] { });
}