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

Commit 24fd1cc

Browse files
committed
Fix creation of types without parameterless constructor on .NET Core
1 parent aa252d8 commit 24fd1cc

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/ServiceStack.Text/ReflectionExtensions.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ public static TypeInfo GetTypeInfo(this Type type)
8989
}
9090
#endif
9191

92+
#if NETSTANDARD
93+
private static readonly Func<Type, object> GetUninitializedObjectDelegate;
94+
95+
static ReflectionExtensions()
96+
{
97+
var formatterServices = typeof(string).GetTypeInfo().Assembly
98+
.GetType("System.Runtime.Serialization.FormatterServices");
99+
if (formatterServices != null)
100+
{
101+
var method = formatterServices.GetMethod("GetUninitializedObject");
102+
if (method != null)
103+
GetUninitializedObjectDelegate = (Func<Type, object>)method.CreateDelegate(typeof(Func<Type, object>));
104+
}
105+
}
106+
#endif
107+
92108
public static TypeCode GetTypeCode(this Type type)
93109
{
94110
#if (NETFX_CORE || PCL || NETSTANDARD)
@@ -522,7 +538,12 @@ public static EmptyCtorDelegate GetConstructorMethodToCache(Type type)
522538
#endif
523539
}
524540

525-
#if (SL5 && !WP) || XBOX || NETSTANDARD
541+
#if (SL5 && !WP) || XBOX
542+
return () => Activator.CreateInstance(type);
543+
#elif NETSTANDARD
544+
if (GetUninitializedObjectDelegate != null)
545+
return () => GetUninitializedObjectDelegate(type);
546+
526547
return () => Activator.CreateInstance(type);
527548
#elif WP || PCL
528549
return System.Linq.Expressions.Expression.Lambda<EmptyCtorDelegate>(

0 commit comments

Comments
 (0)