private static bool InnerConvertObjectToType(object obj, Type type, bool throwOnError,
out object convertedObject)
{
Type originalType = obj.GetType();
OriginalTypeConverter converter = TypeDescriptor.GetConverter(type);
if (converter.CanConvertFrom(originalType))
{
try
{
convertedObject = converter.ConvertFrom(null, CultureInfo.InvariantCulture, obj);
return(true);
}
catch
{
if (throwOnError)
{
throw;
}
convertedObject = null;
return(false);
}
}
if (converter.CanConvertFrom(typeof(string)) && !(obj is DateTime))
{
try
{
string text = TypeDescriptor.GetConverter(originalType).ConvertToInvariantString(obj);
convertedObject = converter.ConvertFromInvariantString(text);
return(true);
}
catch
{
if (throwOnError)
{
throw;
}
convertedObject = null;
return(false);
}
}
if (type.IsInstanceOfType(obj))
{
convertedObject = obj;
return(true);
}
if (throwOnError)
{
throw new InvalidOperationException();
}
convertedObject = null;
return(false);
}