public static void RunTest() { var testHandle = new TestSafeHandle(initialValue); Assert.True(SafeHandleNative.SafeHandleByValue(testHandle, initialValue)); Assert.False(testHandle.IsClosed); Assert.True(SafeHandleNative.SafeHandleByRef(ref testHandle, initialValue, newValue)); Assert.False(testHandle.IsClosed); testHandle = null; SafeHandleNative.SafeHandleOut(out testHandle, initialValue); Assert.False(testHandle.IsClosed); testHandle = SafeHandleNative.SafeHandleReturn(newValue); Assert.False(testHandle.IsClosed); testHandle = SafeHandleNative.SafeHandleReturn_Swapped(newValue); Assert.False(testHandle.IsClosed); var str = new SafeHandleNative.StructWithHandle { handle = new TestSafeHandle(initialValue) }; SafeHandleNative.StructWithSafeHandleByValue(str, initialValue); Assert.False(str.handle.IsClosed); SafeHandleNative.StructWithSafeHandleByRef(ref str, initialValue, initialValue); Assert.False(str.handle.IsClosed); }
public static void RunTest() { var testHandle = new TestSafeHandle(initialValue); Assert.True(SafeHandleNative.SafeHandleByValue(testHandle, initialValue)); Assert.True(SafeHandleNative.SafeHandleByRef(ref testHandle, initialValue, newValue)); Assert.Equal(newValue, testHandle.DangerousGetHandle()); AbstractDerivedSafeHandle abstrHandle = new AbstractDerivedSafeHandleImplementation(initialValue); Assert.True(SafeHandleNative.SafeHandleInByRef(abstrHandle, initialValue)); Assert.Throws <MarshalDirectiveException>(() => SafeHandleNative.SafeHandleByRef(ref abstrHandle, initialValue, newValue)); NoDefaultConstructorSafeHandle noDefaultCtorHandle = new NoDefaultConstructorSafeHandle(initialValue); Assert.Throws <MissingMethodException>(() => SafeHandleNative.SafeHandleByRef(ref noDefaultCtorHandle, initialValue, newValue)); testHandle = null; SafeHandleNative.SafeHandleOut(out testHandle, initialValue); Assert.Equal(initialValue, testHandle.DangerousGetHandle()); testHandle = SafeHandleNative.SafeHandleReturn(newValue); Assert.Equal(newValue, testHandle.DangerousGetHandle()); Assert.Throws <MarshalDirectiveException>(() => SafeHandleNative.SafeHandleReturn_AbstractDerived(initialValue)); Assert.Throws <MissingMethodException>(() => SafeHandleNative.SafeHandleReturn_NoDefaultConstructor(initialValue)); var abstractDerivedImplementationHandle = SafeHandleNative.SafeHandleReturn_AbstractDerivedImplementation(initialValue); Assert.Equal(initialValue, abstractDerivedImplementationHandle.DangerousGetHandle()); testHandle = SafeHandleNative.SafeHandleReturn_Swapped(newValue); Assert.Equal(newValue, testHandle.DangerousGetHandle()); Assert.Throws <MarshalDirectiveException>(() => SafeHandleNative.SafeHandleReturn_Swapped_AbstractDerived(initialValue)); Assert.Throws <MissingMethodException>(() => SafeHandleNative.SafeHandleReturn_Swapped_NoDefaultConstructor(initialValue)); var str = new SafeHandleNative.StructWithHandle { handle = new TestSafeHandle(initialValue) }; Assert.True(SafeHandleNative.StructWithSafeHandleByValue(str, initialValue)); Assert.True(SafeHandleNative.StructWithSafeHandleByRef(ref str, initialValue, initialValue)); // Cannot change the value of a SafeHandle-derived field in a struct when marshalling byref. Assert.Throws <NotSupportedException>(() => SafeHandleNative.StructWithSafeHandleByRef(ref str, initialValue, newValue)); // Cannot create a SafeHandle-derived field value. Assert.Throws <NotSupportedException>(() => SafeHandleNative.StructWithSafeHandleOut(out var defaultOutStruct, initialValue)); }