Skip to content

Commit

Permalink
[Core/Reflection] Fix ProxyObject RuntimeBinding fallback
Browse files Browse the repository at this point in the history
Attempt to initialize the default type with the uninitialized type instance.
  • Loading branch information
Qonfused committed Dec 15, 2023
1 parent 83ca9e0 commit 983026b
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions MTGOSDK/src/Core/Reflection/ProxyObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ public class ProxyObject(
{
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
Type returnType = binder.ReturnType;
dynamic typeDefault = RuntimeHelpers.GetUninitializedObject(returnType);

// First attempt to retrieve the member from the base object.
try
{
Expand All @@ -38,9 +35,24 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
catch(Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)
{
var value = @base.GetType().GetProperty(binder.Name).GetValue(@base);
result = (value != null && value != typeDefault)
? value
: @default ?? value;
try
{
// Get the default value for the return type.
Type returnType = binder.ReturnType;
dynamic typeRef = RuntimeHelpers.GetUninitializedObject(returnType);
dynamic typeDefault = typeRef
.GetType()
.GetConstructor(Type.EmptyTypes)
.Invoke(typeRef, null);
#pragma warning disable CS8601
result = (value != null || value != typeDefault)
? value
: @default ?? value;
#pragma warning restore CS8601
} catch
{
result = value ?? @default;
}

return true;
}
Expand Down

0 comments on commit 983026b

Please sign in to comment.