i'm using this extension method for converting objects in my project. But its unable to convert GUID
as its doesn't implements IConvertible
Interface but for conversion i always have to use new Guid(fooobject)
but i want that i could use this method to convert objects to GUID
. any idea how can we make it flexible to work with GUID
also.
Extension method is
public static T ToType<T>(this object val, T alt) where T : struct, IConvertible
{
try
{
return (T)Convert.ChangeType(val, typeof(T));
}
catch
{
return alt;
}
}