I’m having a problem of InvalidCastException trying to cast an
IList<KeyValuePair<string, object>> x
to an
IList<IItem> y
where IItem is my interface
I have tried…
IList<IItem> y = (IItem) x; //INVALIDCASTEXCEPTION
IList<IItem> y = x.Cast<IItem>().ToList(); //another exception
… someone can help me?
A
KeyValuePar<string,object>cannot be casted to your interface. You need to create instances of a class which implements it, then you can cast it to the interface type.Assuming this is your interface and a class which implements it:
Now you can create a
IList<IItem>from yourList<KeyValuePar<string,object>>: