11
private static void GetData()
{
   dynamic dynamicList =FetchData();
   FilterAndSortDataList(dynamicList);
}

private static void FilterAndSortDataList<T>(List<T> dataList)
{
    ...
}

I am getting a runtime binding error when I call FilterAndSortDataList. Is there a way to cast my dynamicList to List<T> at runtime?

Note that FetchData() is implimented by plugins, so I don't know in advance what the type T is.

BrokeMyLegBiking
  • 5,898
  • 14
  • 51
  • 66
  • It needs to actually *be* a list for that to work. The fact that it gives that error means that the object isn't actually a list. – Servy Aug 09 '13 at 16:06
  • There may be some help here: http://stackoverflow.com/q/5014235 and here: http://stackoverflow.com/questions/513952 – Robert Harvey Aug 09 '13 at 16:18
  • What kind of object is actually in the `dynamic` variable? In other words, what does `FetchData()` return? – Robert Harvey Aug 09 '13 at 17:19

2 Answers2

8

I see no reason why it should fail, unless FetchData is improper data.

Possibility I: FetchData is returning null, and hence the type parameter cannot be figured out (null has no type in C#).

Possibility II: FetchData is not returning a proper List<T> object.

I would redesign the thing like:

private static void GetData()
{
   dynamic dynamicList = FetchData();

   if (dynamicList is IEnumerable) //handles null as well
       FilterAndSortDataList(Enumerable.ToList(dynamicList));

   //throw; //better meaning here.
}

It checks whether the returned type is IEnumerable (hoping that it is some IEnumerable<T> - we cant check if it is IEnumerable<T> itself since we dont have T with us. Its a decent assumption) in which case we convert the obtained sequence to List<T>, just to be sure we are passing a List<T>. dynamic wont work with extension methods, so we have to call Enumerable.ToList unfortunately. In case dynamicList is null or not an enumerable it throws which gives it better meaning than some run time binding error.

nawfal
  • 70,104
  • 56
  • 326
  • 368
0

Is there a way to cast my dynamicList to List at runtime

I don't know why you declare it as dynamic when it has to be a List<T>, but I'm guessing it is because you don't know what T is. If you did, you could just directly cast it:

private static void GetData()
{
   dynamic dynamicList = new List<string> ();
   FilterAndSortDataList((List<string>)dynamicList);
}

private static void FilterAndSortDataList<T>(List<T> dataList)
{
    ...
}

but obviously that could fail at runtime.

Another option might be to make GetData generic as well:

private static void GetData<T>()
{
   List<T> list = new List<T>();
   FilterAndSortDataList(list);
}

private static void FilterAndSortDataList<T>(List<T> dataList)
{
    ...
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • 1
    After `dynamic dynamicList = new List ();` dynamicList is `List` and no need to cast. – Hamlet Hakobyan Aug 09 '13 at 16:16
  • @HamletHakobyan: Yeah, I think D's point is that the element type is not known at compile time. – Robert Harvey Aug 09 '13 at 16:18
  • @RobertHarvey If you don't know element type at compile time you can't cast it directly. :) – Hamlet Hakobyan Aug 09 '13 at 16:20
  • @HamletHakobyan: So true. But there are some magic tricks that might work; see the comments I posted below the OP. – Robert Harvey Aug 09 '13 at 16:21
  • 2
    @RobertHarvey It doesn't matter whether he knows the type at compile time or not, since he's using `dynamic`. Clearly the actual value is an array or a string or something that's not any kind of list, since it failed. If it was any kind of list, even if he doesn't know the generic argument at compile time, then his code would work. – Servy Aug 09 '13 at 16:28
  • @RobertHarvey As I [said from the start](http://stackoverflow.com/questions/18151018/cast-dynamic-to-listt/18151114?noredirect=1#comment26585617_18151018) for it to fail requires the actual object within the variable to not actually be a list. Without knowing what it actually is, we can't know how to turn it into a list. – Servy Aug 09 '13 at 16:35
  • @Servy I did, in fact, agree with you. – Robert Harvey Aug 09 '13 at 16:44
  • since I don't know the type at compile time, this doesn't work. – BrokeMyLegBiking Aug 09 '13 at 16:56
  • @BrokeMyLegBiking: To cast it to a `List` you will have to divine what's in the dynamic, and then perform some sort of reflection magic to reflect over the members and stuff them into a list. – Robert Harvey Aug 09 '13 at 17:05