Skip to content

Commit 70060e9

Browse files
author
ale
committed
处理 nullable GetValueOrDefault
1 parent 552da36 commit 70060e9

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

Source/VSProj/Src/Core/ReflectionMethodInvoker.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ internal class ReflectionMethodInvoker
3434

3535
bool isNullableHasValue = false;
3636
bool isNullableValue = false;
37+
bool isNullableGetValueOrDefault = false;
3738

3839
public ReflectionMethodInvoker(MethodBase method)
3940
{
@@ -75,6 +76,7 @@ public ReflectionMethodInvoker(MethodBase method)
7576
&& method.DeclaringType.GetGenericTypeDefinition() == typeof(Nullable<>);
7677
isNullableHasValue = isNullableMethod && method.Name == "get_HasValue";
7778
isNullableValue = isNullableMethod && method.Name == "get_Value";
79+
isNullableGetValueOrDefault = isNullableMethod && method.Name == "GetValueOrDefault";
7880
}
7981

8082
// #lizard forgives
@@ -156,6 +158,20 @@ public unsafe void Invoke(VirtualMachine virtualMachine, ref Call call, bool isI
156158
{
157159
ret = instance;
158160
}
161+
else if (isNullableGetValueOrDefault)
162+
{
163+
if(instance == null)
164+
{
165+
if(paramCount == 0)
166+
ret = System.Activator.CreateInstance(returnType);
167+
else
168+
ret = args[0];
169+
}
170+
else
171+
{
172+
ret = instance;
173+
}
174+
}
159175
else
160176
{
161177
if (method.IsStatic == false && instance == null)

0 commit comments

Comments
 (0)