I can call Get or Get
But when compiling I get:
Cannot implicitly convert typ
ChangeType is probably your best option. My solution is similar to the one provided by BrokenGlass with a bit of try catch logic.
static void Main(string[] args)
{
object number = "1";
bool hasConverted;
var convertedValue = DoConvert(number, out hasConverted);
Console.WriteLine(hasConverted);
Console.WriteLine(convertedValue);
}
public static TConvertType DoConvert(object convertValue, out bool hasConverted)
{
hasConverted = false;
var converted = default(TConvertType);
try
{
converted = (TConvertType)
Convert.ChangeType(convertValue, typeof(TConvertType));
hasConverted = true;
}
catch (InvalidCastException)
{
}
catch (ArgumentNullException)
{
}
catch (FormatException)
{
}
catch (OverflowException)
{
}
return converted;
}