Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix datapackage to datapackageview invalid cast #903

Merged
merged 5 commits into from
Jul 8, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/WinRT.Runtime/ComWrappersSupport.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ internal static unsafe InspectableInfo GetInspectableInfo(IntPtr pThis)
return InspectableInfoTable.GetValue(_this.GetType(), o => PregenerateNativeTypeInformation(o).inspectableInfo);
}

public static T CreateRcwForComObject<T>(IntPtr ptr)
public static T CreateRcwForComObject<T>(IntPtr ptr)
{
return CreateRcwForComObject<T>(ptr, true);
}

private static T CreateRcwForComObject<T>(IntPtr ptr, bool tryUseCache)
{
if (ptr == IntPtr.Zero)
{
Expand All @@ -83,7 +88,9 @@ public static T CreateRcwForComObject<T>(IntPtr ptr)
// ComWrappers API surface, so we are achieving it via a thread local. We unset it after in case
// there is other calls to it via other means.
CreateRCWType.Value = typeof(T);
var rcw = ComWrappers.GetOrCreateObjectForComInstance(ptr, CreateObjectFlags.TrackerObject);

var flags = tryUseCache ? CreateObjectFlags.TrackerObject : CreateObjectFlags.TrackerObject | CreateObjectFlags.UniqueInstance;
var rcw = ComWrappers.GetOrCreateObjectForComInstance(ptr, flags);
CreateRCWType.Value = null;
// Because .NET will de-duplicate strings and WinRT doesn't,
// our RCW factory returns a wrapper of our string instance.
Expand All @@ -99,12 +106,15 @@ public static T CreateRcwForComObject<T>(IntPtr ptr)
winrtObj.Resurrect();
}

return rcw switch
{
ABI.System.Nullable<string> ns => (T)(object) ns.Value,
ABI.System.Nullable<Type> nt => (T)(object) nt.Value,
_ => (T) rcw
};
return rcw switch
{
ABI.System.Nullable<string> ns => (T)(object)ns.Value,
ABI.System.Nullable<Type> nt => (T)(object)nt.Value,
T castRcw => castRcw,
_ when tryUseCache => CreateRcwForComObject<T>(ptr, false),
_ => throw new ArgumentException(string.Format("Unable to create rcw. The com object {0} has type {1} which cannot be assigned to type {2}", ptr, rcw.GetType(), typeof(T)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_ => throw new ArgumentException(string.Format("Unable to create rcw. The com object {0} has type {1} which cannot be assigned to type {2}", ptr, rcw.GetType(), typeof(T)))
_ => throw new ArgumentException(string.Format("Unable to create a wrapper object. The WinRT object {0} has type {1} which cannot be assigned to type {2}", ptr, rcw.GetType(), typeof(T)))

};

}

public static bool TryUnwrapObject(object o, out IObjectReference objRef)
Expand Down