The List<MyClass>
object can still be just passed to your interface implementation, because List can be implicitly cast down to object
(as can all objects). If the compiler complains because of the way you have your code set up (i have to say that because you haven't actually shown us any code) then you can explicitly cast it:
myInterfaceFunction((object) myList);
or:
myInterfaceFunction(myList as object);
If you want to cast the contents of the list and pass a List<object>
when you already have a List<MyClass>
then you can just do this:
myInterfaceFunction(myList.Cast<object>().ToList());
(you can get rid of the ToList() if your interface function uses IEnumerable<object>
instead of List<object>
).
Here is a quick example. This has a good illustration of my comment about explicitly casting to object
- by default the call to MyFunction(myList)
will go to the MyFunction(IEnumerable<object> blah)
function - you have to explicitly cast it to force it to go to the MyFunction(object blah)
function.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<MyObject> myList = new List<MyObject>();
MyFunction(myList);
MyFunction((object)myList);
MyFunction(myList.Cast<object>().ToList());
MyFunction(myList.Cast<object>());
}
public static void MyFunction(List<object> blah)
{
Console.WriteLine(blah.GetType().ToString());
}
public static void MyFunction(IEnumerable<object> blah)
{
Console.WriteLine(blah.GetType().ToString());
}
public static void MyFunction(object blah)
{
Console.WriteLine(blah.GetType().ToString());
}
}
public class MyObject { }
}