Skip to content

Commit

Permalink
Remove out of date duplicate TryUnwrapObject implementation. (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkoritzinsky authored Apr 28, 2020
1 parent 312b730 commit 45f9d5b
Showing 1 changed file with 1 addition and 31 deletions.
32 changes: 1 addition & 31 deletions WinRT.Runtime/Marshalers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ public static IObjectReference CreateMarshaler(object o, bool unwrapObject = tru
return null;
}

if (unwrapObject && TryUnwrapObject(o, out var objRef))
if (unwrapObject && ComWrappersSupport.TryUnwrapObject(o, out var objRef))
{
return objRef.As<IInspectable.Vftbl>();
}
Expand Down Expand Up @@ -975,36 +975,6 @@ public static unsafe void CopyManaged(object o, IntPtr dest, bool unwrapObject =
*(IntPtr*)dest.ToPointer() = objRef?.GetRef() ?? IntPtr.Zero;
}

private static bool TryUnwrapObject(object o, out IObjectReference objRef)
{
// The unwrapping here needs to be in exact type match in case the user
// has implemented a WinRT interface or inherited from a WinRT class
// in a .NET (non-projected) type.

// TODO: Define and output attributes defining that a type is a projected interface
// or class type to avoid accidental collisions.
// Also, it might be a good idea to add a property to get the IObjectReference
// that is marked [EditorBrowsable(EditorBrowsableState.Never)] to hide it from most IDEs
// to help avoid using private implementation details.
Type type = o.GetType();
// Projected interface types have fields name _obj that hold their object reference.
objRef = (IObjectReference)type.GetField("_obj", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)?.GetValue(o);
if (objRef != null)
{
return true;
}

// If we get here, we're either a class or a non-WinRT type. If we're a class, we'll have a _default field holding a reference to our default interface.
object defaultInterface = type.GetField("_default", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)?.GetValue(o);
if (defaultInterface != null)
{
return TryUnwrapObject(defaultInterface, out objRef);
}

objRef = null;
return false;
}

public static unsafe MarshalInterfaceHelper<object>.MarshalerArray CreateMarshalerArray(object[] array) => MarshalInterfaceHelper<object>.CreateMarshalerArray(array, (o) => CreateMarshaler(o));

public static (int length, IntPtr data) GetAbiArray(object box) => MarshalInterfaceHelper<object>.GetAbiArray(box);
Expand Down

0 comments on commit 45f9d5b

Please sign in to comment.