Skip to content

Commit

Permalink
处理 nullable GetValueOrDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
ale committed Sep 1, 2023
1 parent 552da36 commit 70060e9
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Source/VSProj/Src/Core/ReflectionMethodInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal class ReflectionMethodInvoker

bool isNullableHasValue = false;
bool isNullableValue = false;
bool isNullableGetValueOrDefault = false;

public ReflectionMethodInvoker(MethodBase method)
{
Expand Down Expand Up @@ -75,6 +76,7 @@ public ReflectionMethodInvoker(MethodBase method)
&& method.DeclaringType.GetGenericTypeDefinition() == typeof(Nullable<>);
isNullableHasValue = isNullableMethod && method.Name == "get_HasValue";
isNullableValue = isNullableMethod && method.Name == "get_Value";
isNullableGetValueOrDefault = isNullableMethod && method.Name == "GetValueOrDefault";
}

// #lizard forgives
Expand Down Expand Up @@ -156,6 +158,20 @@ public unsafe void Invoke(VirtualMachine virtualMachine, ref Call call, bool isI
{
ret = instance;
}
else if (isNullableGetValueOrDefault)
{
if(instance == null)
{
if(paramCount == 0)
ret = System.Activator.CreateInstance(returnType);
else
ret = args[0];
}
else
{
ret = instance;
}
}
else
{
if (method.IsStatic == false && instance == null)
Expand Down

0 comments on commit 70060e9

Please sign in to comment.