Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5668,6 +5668,7 @@ public static void PrepareMethod(System.RuntimeMethodHandle method, System.Runti
public static void ProbeForSufficientStack() { }
#if netcoreapp11
public static bool TryEnsureSufficientExecutionStack() { return default(bool); }
public static object GetUninitializedObject(Type type) { return default(object); }
#endif
}
[System.AttributeUsageAttribute((System.AttributeTargets)(64), Inherited = false, AllowMultiple = false)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,35 @@ private static void FillStack(int depth)
FillStack(depth + 1);
}
}

[Fact]
public static void GetUninitializedObject_InvalidArguments_ThrowsException()
{
Assert.Throws<ArgumentNullException>("type", () => RuntimeHelpers.GetUninitializedObject(null));

Assert.Throws<ArgumentException>(() => RuntimeHelpers.GetUninitializedObject(typeof(string))); // special type
Assert.Throws<MemberAccessException>(() => RuntimeHelpers.GetUninitializedObject(typeof(System.IO.Stream))); // abstract type
Assert.Throws<MemberAccessException>(() => RuntimeHelpers.GetUninitializedObject(typeof(System.Collections.IEnumerable))); // interface
Assert.Throws<MemberAccessException>(() => RuntimeHelpers.GetUninitializedObject(typeof(System.Collections.Generic.List<>))); // generic definition
}

[Fact]
public static void GetUninitializedObject_DoesNotRunConstructor()
{
Assert.Equal(42, new ObjectWithDefaultCtor().Value);
Assert.Equal(0, ((ObjectWithDefaultCtor)RuntimeHelpers.GetUninitializedObject(typeof(ObjectWithDefaultCtor))).Value);
}

[Fact]
public static void GetUninitializedObject_Nullable()
{
// Nullable returns the underlying type instead
Assert.Equal(typeof(int), RuntimeHelpers.GetUninitializedObject(typeof(Nullable<int>)).GetType());
}

private class ObjectWithDefaultCtor
{
public int Value = 42;
}
}
}