[ObjCRuntime] Add explicit conversion operators between NativeHandle and void*. Fixes #13867. (#13929)

Fixes https://github.com/xamarin/xamarin-macios/issues/13867.
This commit is contained in:
Rolf Bjarne Kvinge 2022-01-28 10:03:54 +01:00 коммит произвёл GitHub
Родитель d2ef29739b
Коммит 43d74fb895
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 36 добавлений и 0 удалений

Просмотреть файл

@ -66,6 +66,16 @@ namespace ObjCRuntime {
return new NativeHandle (value); return new NativeHandle (value);
} }
public unsafe static explicit operator void* (NativeHandle value)
{
return (void *) (IntPtr) value;
}
public unsafe static explicit operator NativeHandle (void * value)
{
return new NativeHandle ((IntPtr) value);
}
public override bool Equals (object? o) public override bool Equals (object? o)
{ {
if (o is NativeHandle nh) if (o is NativeHandle nh)

Просмотреть файл

@ -0,0 +1,26 @@
using System;
using Foundation;
using ObjCRuntime;
using NUnit.Framework;
#if NET
namespace MonoTouchFixtures.ObjCRuntime {
[TestFixture]
[Preserve (AllMembers = true)]
public class NativeHandleTest {
[Test]
public unsafe void Operators ()
{
IntPtr value = new IntPtr (0xdadf00d);
Assert.AreEqual (value, ((NativeHandle) value).Handle, "IntPtr -> NativeHandle");
Assert.AreEqual (value, (IntPtr) new NativeHandle (value), "NativeHandle -> IntPtr");
Assert.AreEqual (value, ((NativeHandle) ((void *) value)).Handle, "void* -> NativeHandle");
Assert.AreEqual (value, (IntPtr) (void *) new NativeHandle (value), "NativeHandle -> void*");
}
}
}
#endif