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

Add ObjectDisposedException.Throw for object instance and type #58684

Merged
merged 21 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Diagnostics.CodeAnalysis;

namespace System
{
Expand Down Expand Up @@ -44,6 +45,14 @@ protected ObjectDisposedException(SerializationInfo info, StreamingContext conte
_objectName = info.GetString("ObjectName");
}

[DoesNotReturn]
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
public static void Throw(object instance) =>
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
Throw(instance.GetType());
jeffhandley marked this conversation as resolved.
Show resolved Hide resolved

[DoesNotReturn]
public static void Throw(Type type) =>
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
throw new ObjectDisposedException(type.FullName);
jeffhandley marked this conversation as resolved.
Show resolved Hide resolved

public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
Expand Down
4 changes: 4 additions & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4935,6 +4935,10 @@ public ObjectDisposedException(string? message, System.Exception? innerException
public ObjectDisposedException(string? objectName, string? message) { }
public override string Message { get { throw null; } }
public string ObjectName { get { throw null; } }
[System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute]
public static void Throw(System.Object instance) => throw null;
[System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute]
public static void Throw(System.Type type) => throw null;
public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
}
[System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,19 @@ public static void Ctor_String_String()
Assert.Contains(message, exception.Message);
Assert.Contains(objectName, exception.Message);
}

[Fact]
public static void Throw_Object()
{
var obj = new object();
AssertExtensions.Throws<ObjectDisposedException>(() => ObjectDisposedException.Throw(obj));
bartonjs marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public static void Throw_Type()
{
var type = new object().GetType();
bartonjs marked this conversation as resolved.
Show resolved Hide resolved
AssertExtensions.Throws<ObjectDisposedException>(() => ObjectDisposedException.Throw(type));
}
}
}