-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathTaskInfo.cs
More file actions
152 lines (121 loc) · 4.75 KB
/
TaskInfo.cs
File metadata and controls
152 lines (121 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Gofer.NET.Utils.Errors;
namespace Gofer.NET.Utils
{
public class TaskInfo
{
public string Id { get; set; }
public string AssemblyName { get; set; }
public string TypeName { get; set; }
public string MethodName { get; set; }
public object[] Args { get; set; }
public Type[] ArgTypes { get; set; }
public Type ReturnType { get; set; }
public DateTime CreatedAtUtc { get; set; }
public bool IsExpired(TimeSpan expirationSpan)
{
return CreatedAtUtc < (DateTime.UtcNow - expirationSpan);
}
public bool IsEquivalent(TaskInfo otherTaskInfo)
{
if (otherTaskInfo == null)
{
throw new ArgumentException("otherTaskInfo must not be null");
}
if (Args.Length != otherTaskInfo.Args.Length)
{
return false;
}
for (var i=0; i<Args.Length; ++i) {
if (!Args[i].Equals(otherTaskInfo.Args[i]))
{
return false;
}
}
return string.Equals(AssemblyName, otherTaskInfo.AssemblyName, StringComparison.Ordinal)
&& string.Equals(TypeName, otherTaskInfo.TypeName, StringComparison.Ordinal)
&& string.Equals(MethodName, otherTaskInfo.MethodName, StringComparison.Ordinal)
&& ReturnType.Equals(otherTaskInfo.ReturnType);
}
public void ConvertTypeArgs()
{
for (var i=0;i<Args.Length;++i) {
if (Args[i] == null)
continue;
if (typeof(Type).IsInstanceOfType(Args[i]))
{
Args[i] = new TypeWrapper { Type = (Type)Args[i] };
}
}
}
public void UnconvertTypeArgs()
{
for (var i=0;i<Args.Length;++i) {
if (Args[i] == null)
continue;
var argType = Nullable.GetUnderlyingType(ArgTypes[i]) ?? ArgTypes[i];
if (typeof(TypeWrapper).IsInstanceOfType(Args[i]))
{
Args[i] = ((TypeWrapper)Args[i]).Type;
}
else if (typeof(TimeSpan).IsAssignableFrom(argType))
{
Args[i] = TimeSpan.Parse((string)Args[i]);
}
else if (typeof(DateTime).IsAssignableFrom(argType) && Args[i] is DateTimeOffset dateTimeOffset)
{
Args[i] = dateTimeOffset.DateTime;
}
}
}
private async Task<object> InvokeMethod(MethodInfo method, object instance)
{
if (method.IsAsync())
{
var result = method.Invoke(instance, Args);
var task = (Task) result;
await task;
var resultProperty = task.GetType().GetProperty("Result");
var resultValue = resultProperty.GetValue(task);
// Return null if the method is a Task<void> equivalent
var resultType = resultValue.GetType();
if (resultType.Name.Equals("VoidTaskResult", StringComparison.Ordinal)
&& resultType.Namespace.Equals("System.Threading.Tasks", StringComparison.Ordinal))
{
return null;
}
return resultValue;
}
return method.Invoke(instance, Args);
}
public async Task<object> ExecuteTask()
{
var assembly = Assembly.Load(AssemblyName);
var type = assembly.GetType(TypeName);
var staticMethod = type.GetMethod(MethodName,
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic,
null,
ArgTypes,
null);
if (staticMethod != null)
{
return await InvokeMethod(staticMethod, null);
}
var instanceMethod = type.GetMethod(MethodName,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
null,
ArgTypes,
null);
if (instanceMethod == null)
{
throw new UnableToDeserializeDelegateException();
}
var instance = Activator.CreateInstance(type);
return await InvokeMethod(instanceMethod, instance);
}
}
}