4

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;
        }
    }
Mayank Pathak
  • 3,621
  • 5
  • 39
  • 67
  • You can always special-case checking `typeof(T)` .. although GUID does have [its own TypeConverter](http://msdn.microsoft.com/en-us/library/system.componentmodel.guidconverter.aspx). You will likely need to convert `val` to a `string`. Otherwise, see the example on the page. –  Aug 06 '12 at 07:03
  • yes `TypeConverter` is not there for GUID as it's value type i guess. we need to unbox explicitly always to convert it to GUID. that's why i want to do this using Extension method... – Mayank Pathak Aug 06 '12 at 07:06
  • I am pretty sure it'll still work .. if `val` is *not* a GUID value then you'll need to Convert it. If it is a GUID you can tell with a `typeof(T)` check and cast is directly. –  Aug 06 '12 at 07:07
  • @pst No sir, it doesn't work and shows error unable to implicitly conversion error...You can try it by making a simple example... – Mayank Pathak Aug 06 '12 at 07:13
  • 1
    Oh, I see. The problem is the constraints of `T` are *too restrictive* with `IConvertible` (it has nothing to do with the stuff *in* the method). You'll need to relax them (and do the checks inside the method) or create an overload. (Although I am not sure if an method can be overloaded based on type refinement of a generic ..) –  Aug 06 '12 at 07:15
  • Yes it can be overloaded..i have overloaded but i just want to make it sure if it can be done with this method...Also it doesn't make sense for me to add special overloaded method only for GUID... – Mayank Pathak Aug 06 '12 at 07:19
  • It isn't just GUID .. it's actually most types outside of the "primitive" values. Perhaps have `CoerceToType` (IConvertible) and `ConvertToType` (TypeConverter), as two different methods? –  Aug 06 '12 at 07:25

1 Answers1

2

Because you have the constraint that the type you are converting to implements the IConvertible interface (whereas the Guid structure) does not, you have no choice to create an overload, like this:

public static Guid ToType(this object val, Guid alt)
{
    try
    {
        // Convert here.
    }
    catch
    {
        return alt;
    }
}

When you pass a Guid, it will resolve because of section 7.4.2 of the C# specification (emphasis mine):

Once the candidate function members and the argument list have been identified, the selection of the best function member is the same in all cases:

  • Given the set of applicable candidate function members, the best function member in that set is located.

Given that Guid is a more specific match than the type parameter T, your second method will be called.

Note, if you removed the IConvertible interface constraint, you could handle this in a single method, but you'd have to be able to have logic to handle any structure that is passed for T (a TypeConverter instance would be handy here).

casperOne
  • 73,706
  • 19
  • 184
  • 253