Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object must implement IConvertible when trying to return a tuple

Tags:

c#

At runtime I get the following error

"Object must implement IConvertible"

calling function

lboxBuildingType.SelectedIndex = pharse.returning<int>xdoc.Root.Element("BuildingTypeIndex").Value);

public static T returning<T>(object o)
{
       Tuple<bool, T, object> tmp;
       switch (Type.GetTypeCode(typeof(T)))
       {
        ////blah blah blah
           case TypeCode.Int32:
              tmp= (Tuple<bool,T,object>)Convert.ChangeType(I(o.ToString())), typeof(T)); // error
              break;
        ////blah blah blah
       }
}

private static Tuple<bool, Int32, Object> I(object o)
{
      int i;
      bool b;
      Int32.TryParse(o.ToString(), out i);
      b = (i == 0);
      return new Tuple<bool, Int32, object>(b, i, o);
}

The aim of the code is to pass in a <T>("15") and have it produce a tuple<Bool,T,object> which would be tuple<true, 15, "15">

It errors out where I have marked it with a // error

like image 575
CyberTYK Avatar asked Oct 19 '15 14:10

CyberTYK


2 Answers

ConvertType is a method that lets you convert objects implementing IConvertable into one of a fixed set of objects (strings, numeric types, etc.) Not only is it not able to convert any IConvertible object into any type of Tuple (If you look at the methods of that interface you'll see why.) but the Tuple that you're calling it on isn't IConvertible as the error message is telling you.

Of course, the solution is to just not call ChangeType in the first place. It exists to convert objects from one type to another, but the object you have is already of the proper type, you just need to inform the compiler that the compile time expression should be different, and that you know that the type will always match at runtime. You do this with just a regular cast:

tmp = (Tuple<bool,T,object>) (object) I(o.ToString());
like image 80
Servy Avatar answered Oct 07 '22 14:10

Servy


Try This. This ignores "Object must implement IConvertible" error for example GUID:

public object ChangeType(object value, Type type)
    {
        if (value == null && type.IsGenericType) return Activator.CreateInstance(type);
        if (value == null) return null;
        if (type == value.GetType()) return value;
        if (type.IsEnum)
        {
            if (value is string)
                return Enum.Parse(type, value as string);
            else
                return Enum.ToObject(type, value);
        }
        if (!type.IsInterface && type.IsGenericType)
        {
            Type innerType = type.GetGenericArguments()[0];
            object innerValue = ChangeType(value, innerType);
            return Activator.CreateInstance(type, new object[] { innerValue });
        }
        if (value is string && type == typeof(Guid)) return new Guid(value as string);
        if (value is string && type == typeof(Version)) return new Version(value as string);
        if (!(value is IConvertible)) return value;
        return Convert.ChangeType(value, type);
    } 
like image 23
nazim hatipoglu Avatar answered Oct 07 '22 12:10

nazim hatipoglu