Skip to content

Commit

Permalink
Random object initialization change.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zapov committed Nov 12, 2014
1 parent 011b46d commit a52a930
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Code/Core/Revenj.Utility/TemporaryResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ private static void InitializeDirectory()
catch { }
});
}
private static Type[] EmptyTypes = new Type[0];
/// <summary>
/// Create instance of specified type and populate it with
/// random values.
Expand All @@ -193,7 +194,9 @@ private static void InitializeDirectory()
/// <returns>object instance</returns>
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())
{
Expand Down

0 comments on commit a52a930

Please sign in to comment.