I have a method which takes (object value) and convert it to string with some tricky rules.
One of those rules is related to when value is IEnumerable. In this case i need to process each item in enum:
public string Convert(object value)
{
var valuetype = value.GetType();
if (valuetype.GetInterface("IList") != null)
{
var e = (IEnumerable<object>) value;
return e.Count() == 0 ?
"" :
e.Select(o=>Convert(o)).Aggregate("", (c, s) => c+s);
}
}
Of course, if value is List<string>, for example, line
var e = (IEnumerable<object>) value;
throw an Exception
Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'System.Collections.Generic.IEnumerable`1[System.Object]'.
Any ideas, how can I get rid of it?
Something like
var e = ((IEnumerable) value).Cast<object>()However I think this may fit the bill: