The most common is probably to use a static factory method:
public class object_t
{
public static object_t CreateObjectT(string config, object_t default_obj)
{
object_t theconfiguredobject = new object_t();
//try to configure it
if( /*failed to initialize from config*/ )
{
return default_obj.Clone();
}
else
{
return theconfiguredobject;
}
}
}
A better way to do the above would be to create a copy constructor:
public object_t (object_t obj)
: this()
{
this.prop1 = obj.prop1;
this.prop2 = obj.prop2;
//...
}
and a method that tries to create your object from the config string:
private static bool TryCreateObjectT(string config, out object_t o)
{
//try to configure the object o
//if it succeeds, return true; else return false
}
then have your factory method call the TryCreateObjectT first, and if it fails, the copy constructor:
public static object_t CreateObjectT(string config, object_t default_obj)
{
object_t o;
return TryCreateObjectT(config, out o) ? o : new object_t(default_obj);
}