Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 6dfba87

Browse files
committed
Fix CreateInstance of string on .NET Core
1 parent 6dbfcb9 commit 6dfba87

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/ServiceStack.Text/ReflectionExtensions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,11 @@ public static EmptyCtorDelegate GetConstructorMethod(string typeName)
466466

467467
public static EmptyCtorDelegate GetConstructorMethodToCache(Type type)
468468
{
469-
if (type.IsInterface())
469+
if (type == typeof(string))
470+
{
471+
return () => String.Empty;
472+
}
473+
else if (type.IsInterface())
470474
{
471475
if (type.HasGenericType())
472476
{
@@ -550,9 +554,6 @@ public static EmptyCtorDelegate GetConstructorMethodToCache(Type type)
550554
return System.Linq.Expressions.Expression.Lambda<EmptyCtorDelegate>(
551555
System.Linq.Expressions.Expression.New(type)).Compile();
552556
#else
553-
if (type == typeof(string))
554-
return () => String.Empty;
555-
556557
//Anonymous types don't have empty constructors
557558
return () => FormatterServices.GetUninitializedObject(type);
558559
#endif

tests/ServiceStack.Text.Tests/ReflectionExtensionTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ public void Only_serializes_public_readable_properties()
5353
Serialize(model);
5454
}
5555

56+
[Test]
57+
public void Can_create_instance_of_string()
58+
{
59+
Assert.That(typeof(string).CreateInstance(), Is.EqualTo(String.Empty));
60+
}
61+
5662
[Test]
5763
public void Can_create_instances_of_common_collections()
5864
{

0 commit comments

Comments
 (0)