I am facing an issue while casting an API response type. The API returns List<object>
in a particular format code given below
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
dynamic tupleStringList;
tupleStringList = new List<dynamic>{Tuple.Create("a","b"),Tuple.Create("c","d")}; // this data will come API which I have no control
IEnumerable<Tuple<string,string>> test = (IEnumerable<Tuple<string,string>>) tupleStringList;// here we need to cast as another internal function which use it as IEnumerable<Tuple<string,string>>
}
}
The error I am getting is given below
Run-time exception (line 9): Unable to cast object of type
'System.Collections.Generic.List`1[System.Object]'
to type'System.Collections.Generic.IEnumerable`1[System.Tuple`2[System.String,System.String]]'
.Stack Trace:
[System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[System.Object]'`` to type 'System.Collections.Generic.IEnumerable`1[System.Tuple`2[System.String,System.String]]'.] at CallSite.Target(Closure,CallSite,Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) at Program.Main() :line 9