/// <summary>
        /// Acquires a lease on a safe handle. This method is intended to be used in the initializer of a <c>using</c>
        /// statement.
        /// </summary>
        /// <param name="handle">The <see cref="SafeHandle"/> to lease.</param>
        /// <returns>A <see cref="SafeHandleLease"/>, which must be disposed to release the resource.</returns>
        /// <exception cref="ObjectDisposedException">If the lease could not be acquired.</exception>
        public static SafeHandleLease Lease(this SafeHandle handle)
        {
            if (handle is null)
            {
                throw new ArgumentNullException(nameof(handle));
            }

            var success = false;

            try
            {
                handle.DangerousAddRef(ref success);
                if (!success)
                {
                    throw new ObjectDisposedException(handle.GetType().FullName);
                }

                return(new SafeHandleLease(handle));
            }
            catch
            {
                if (success)
                {
                    handle.DangerousRelease();
                }

                throw;
            }
        }
Exemplo n.º 2
0
        public static SafeHandleLease Lease(this SafeHandle handle)
        {
            RoslynDebug.AssertNotNull(handle);

            var success = false;

            try
            {
                handle.DangerousAddRef(ref success);
                if (!success)
                {
                    throw new ObjectDisposedException(handle.GetType().FullName);
                }

                return(new SafeHandleLease(handle));
            }
            catch
            {
                if (success)
                {
                    handle.DangerousRelease();
                }

                throw;
            }
        }
        public static object Format(object value)
        {
            // If it's null, return a known string for null values
            if (value == null)
            {
                return(NullInstance);
            }

            // Give another partial implementation a chance to provide its own string representation
            string result = null;

            AdditionalCustomizedToString(value, ref result);
            if (result != null)
            {
                return(result);
            }

            // Format arrays with their element type name and length
            Array arr = value as Array;

            if (arr != null)
            {
                return($"{arr.GetType().GetElementType()}[{((Array)value).Length}]");
            }

            // Format ICollections as the name and count
            ICollection c = value as ICollection;

            if (c != null)
            {
                return($"{c.GetType().Name}({c.Count})");
            }

            // Format SafeHandles as their type, hash code, and pointer value
            SafeHandle handle = value as SafeHandle;

            if (handle != null)
            {
                return($"{handle.GetType().Name}:{handle.GetHashCode()}(0x{handle.DangerousGetHandle():X})");
            }

            // Format IntPtrs as hex
            if (value is IntPtr)
            {
                return($"0x{value:X}");
            }

            // If the string representation of the instance would just be its type name,
            // use its id instead.
            string toString = value.ToString();

            if (toString == null || toString == value.GetType().FullName)
            {
                return(IdOf(value));
            }

            // Otherwise, return the original object so that the caller does default formatting.
            return(value);
        }