diff --git a/src/System.Runtime/ref/System.Runtime.cs b/src/System.Runtime/ref/System.Runtime.cs index 58ed01fb69a3..37583cb3ca0c 100644 --- a/src/System.Runtime/ref/System.Runtime.cs +++ b/src/System.Runtime/ref/System.Runtime.cs @@ -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)] diff --git a/src/System.Runtime/tests/System/Runtime/CompilerServices/RuntimeHelpersTests.netcoreapp1.1.cs b/src/System.Runtime/tests/System/Runtime/CompilerServices/RuntimeHelpersTests.netcoreapp1.1.cs index df2c76213d36..160973fbe435 100644 --- a/src/System.Runtime/tests/System/Runtime/CompilerServices/RuntimeHelpersTests.netcoreapp1.1.cs +++ b/src/System.Runtime/tests/System/Runtime/CompilerServices/RuntimeHelpersTests.netcoreapp1.1.cs @@ -36,5 +36,35 @@ private static void FillStack(int depth) FillStack(depth + 1); } } + + [Fact] + public static void GetUninitializedObject_InvalidArguments_ThrowsException() + { + Assert.Throws("type", () => RuntimeHelpers.GetUninitializedObject(null)); + + Assert.Throws(() => RuntimeHelpers.GetUninitializedObject(typeof(string))); // special type + Assert.Throws(() => RuntimeHelpers.GetUninitializedObject(typeof(System.IO.Stream))); // abstract type + Assert.Throws(() => RuntimeHelpers.GetUninitializedObject(typeof(System.Collections.IEnumerable))); // interface + Assert.Throws(() => 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)).GetType()); + } + + private class ObjectWithDefaultCtor + { + public int Value = 42; + } } }