From a52a9304d8040f0e65d72251611feb158805fbb1 Mon Sep 17 00:00:00 2001 From: Rikard Pavelic Date: Wed, 12 Nov 2014 15:18:41 +0100 Subject: [PATCH] Random object initialization change. Baked in serialization expects correct nullability which is lost in object is created using FormatterServices.GetUninitializedObject Create new instance with empty ctor if it exists. --- Code/Core/Revenj.Utility/TemporaryResources.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Code/Core/Revenj.Utility/TemporaryResources.cs b/Code/Core/Revenj.Utility/TemporaryResources.cs index 2b29ac97..3acc54c3 100644 --- a/Code/Core/Revenj.Utility/TemporaryResources.cs +++ b/Code/Core/Revenj.Utility/TemporaryResources.cs @@ -185,6 +185,7 @@ private static void InitializeDirectory() catch { } }); } + private static Type[] EmptyTypes = new Type[0]; /// /// Create instance of specified type and populate it with /// random values. @@ -193,7 +194,9 @@ private static void InitializeDirectory() /// object instance public static object CreateRandomObject(Type target) { - object instance = FormatterServices.GetUninitializedObject(target); + var ctor = target.GetConstructor(EmptyTypes); + //baked in serialization doesn't like uninitialized objects. + object instance = ctor != null ? ctor.Invoke(null) : FormatterServices.GetUninitializedObject(target); var rnd = new Random(); foreach (var p in instance.GetType().GetProperties()) {