Skip to content

Commit d632aba

Browse files
committed
Fixed #254
1 parent a0a6992 commit d632aba

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/DotNext/Reflection/TaskType.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System.Diagnostics;
12
using System.Diagnostics.CodeAnalysis;
3+
using System.Reflection;
24
using static InlineIL.IL;
35
using static InlineIL.IL.Emit;
46
using static InlineIL.MethodRef;
@@ -20,11 +22,16 @@ private static class Cache<T>
2022

2123
static Cache()
2224
{
23-
Ldnull();
24-
Ldftn(PropertyGet(Type<Task<T>>(), nameof(Task<T>.Result)));
25-
Newobj(Constructor(Type<Func<Task<T>, T>>(), Type<object>(), Type<IntPtr>()));
26-
Pop(out Func<Task<T>, T> propertyGetter);
27-
ResultGetter = propertyGetter;
25+
Ldtoken(PropertyGet(Type<Task<T>>(), nameof(Task<T>.Result)));
26+
Pop(out RuntimeMethodHandle getterHandle);
27+
28+
Ldtoken<Task<T>>();
29+
Pop(out RuntimeTypeHandle taskHandle);
30+
31+
var getterInfo = MethodBase.GetMethodFromHandle(getterHandle, taskHandle) as MethodInfo;
32+
Debug.Assert(getterInfo is not null);
33+
34+
ResultGetter = getterInfo.CreateDelegate<Func<Task<T>, T>>();
2835
}
2936
}
3037

@@ -34,11 +41,16 @@ static TaskType()
3441
{
3542
CompletedTaskType = Task.CompletedTask.GetType();
3643

37-
Ldnull();
38-
Ldftn(PropertyGet(Type<Task>(), nameof(Task.IsCompletedSuccessfully)));
39-
Newobj(Constructor(Type<Func<Task, bool>>(), Type<object>(), Type<IntPtr>()));
40-
Pop(out Func<Task, bool> propertyGetter);
41-
IsCompletedSuccessfullyGetter = propertyGetter;
44+
Ldtoken(PropertyGet(Type<Task>(), nameof(Task.IsCompletedSuccessfully)));
45+
Pop(out RuntimeMethodHandle getterHandle);
46+
47+
Ldtoken<Task>();
48+
Pop(out RuntimeTypeHandle taskHandle);
49+
50+
var getterInfo = MethodBase.GetMethodFromHandle(getterHandle, taskHandle) as MethodInfo;
51+
Debug.Assert(getterInfo is not null);
52+
53+
IsCompletedSuccessfullyGetter = getterInfo.CreateDelegate<Func<Task, bool>>();
4254
}
4355

4456
/// <summary>

0 commit comments

Comments
 (0)