I'm working on a feature request for a .NET test data builder. I need to be able to instantiate classes that only have private parameterless constructors.
For instance, I might need to instantiate the following class:
public class MyClass()
{
private MyClass(){}
}
For most other classes I use the following code:
(T)Activator.CreateInstance(typeof(T), true)
But for classes that only have a private parameterless constructor I use the following:
(T)FormatterServices.GetUninitializedObject(typeof(T))
Unfortunately, I also have the requirement that this work in Silverlight and unfortunately Silverlight doesn't currently contain System.Runtime.Serialization.FormatterServices.
Does anyone know of anything in the Silverlight implementation that would allow me to get around this? Failing that, does anyone know how I might implement my own version of this method?