From 5d88dc2002a05bc1590194232238ed24c3d7cfd1 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Thu, 6 Oct 2016 06:19:50 -0700 Subject: [PATCH] Add RuntimeHelpers.GetUninitializedObject for netcoreapp1.1 --- src/System.Runtime/ref/System.Runtime.cs | 1 + .../RuntimeHelpersTests.netcoreapp1.1.cs | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+) 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; + } } }