Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Merge pull request #8570 from danmosemsft/danmosegetuninit
Browse files Browse the repository at this point in the history
Add FormatterServices.GetUninitializedObject, partial for #8133
  • Loading branch information
danmoseley committed May 16, 2016
2 parents eeddd33 + 893414f commit 112ff9a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3054,6 +3054,13 @@ public StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind layoutKin
public System.Runtime.InteropServices.LayoutKind Value { get { return default(System.Runtime.InteropServices.LayoutKind); } }
}
}
namespace System.Runtime.Serialization
{
public static partial class FormatterServices
{
public static Object GetUninitializedObject(Type type) { return default(Object); }
}
}
namespace System.Runtime.Versioning
{
[System.AttributeUsageAttribute((System.AttributeTargets)(1), AllowMultiple = false, Inherited = false)]
Expand Down
1 change: 1 addition & 0 deletions src/System.Runtime/tests/System.Runtime.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Compile Include="System\Runtime\CompilerServices\ConditionalWeakTableTests.cs" />
<Compile Include="System\Runtime\CompilerServices\StrongBoxTests.cs" />
<Compile Include="System\Runtime\CompilerServices\RuntimeHelpersTests.cs" />
<Compile Include="System\Runtime\Serialization\FormatterServices.cs" />
<Compile Include="System\SByteTests.cs" />
<Compile Include="System\SingleTests.cs" />
<Compile Include="System\StringTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using Xunit;

namespace System.Tests
{
public static class FormatterServicesTests
{
[Fact]
public static void NoArgument_Throws_ArgumentNullException()
{
Assert.Throws<ArgumentNullException>("type", () => FormatterServices.GetUninitializedObject(null));
}

[Theory]
[InlineData(typeof(string))]
[InlineData(typeof(int*))]
public static void StringsAndPointers_Throw_ArgumentException(Type type)
{
Assert.Throws<ArgumentException>(null /* really should be 'type' */, () => FormatterServices.GetUninitializedObject(type));
}

[Fact]
public static void InstantiatedArrays_Throw_ArgumentException()
{
Assert.Throws<ArgumentException>(null /* really should be 'type' */, () => FormatterServices.GetUninitializedObject((new int[] { }).GetType()));
}

[Theory]
[InlineData(typeof(Array))]
[InlineData(typeof(ICollection))]
[InlineData(typeof(Stream))]
public static void InterfacesAndAbstractClasses_Throw_MemberAccessException(Type type)
{
Assert.Throws<MemberAccessException>(() => FormatterServices.GetUninitializedObject(type));
}

[Theory]
[InlineData(typeof(object))]
[InlineData(typeof(MyClass))]
public static void PlainObjects_Success(Type type)
{
Assert.Equal(type, FormatterServices.GetUninitializedObject(type).GetType());
}

[Fact]
public static void Generic_Success()
{
var type = typeof(List<int>);
Assert.Equal(0, ((List<int>)FormatterServices.GetUninitializedObject(type)).Count);
Assert.Equal(type, FormatterServices.GetUninitializedObject(type).GetType());
}

[Theory]
[InlineData(typeof(int), 0)]
[InlineData(typeof(short), 0)]
public static void PrimitiveTypes_Success(Type type, object value)
{
Assert.Equal(value.ToString(), FormatterServices.GetUninitializedObject(type).ToString());
Assert.Equal(type, FormatterServices.GetUninitializedObject(type).GetType());
}

[Fact]
public static void Nullable_BecomesNonNullable_Success()
{
Assert.Equal(typeof(int), FormatterServices.GetUninitializedObject(typeof(int?)).GetType());
}

[Fact]
public static void Result_Is_Mutable()
{
// Sanity check the object is actually useable
MyClass mc = ((MyClass)FormatterServices.GetUninitializedObject(typeof(MyClass)));
mc.MyMember = "foo";
Assert.Equal("foo", mc.MyMember);
}

private class MyClass
{
public string MyMember { get; set; }
}
}
}

0 comments on commit 112ff9a

Please sign in to comment.