I am trying to call a class method through reflection.
public class A
{
public string B(IWork WorkToDo)
{
return WorkToDo.ToString();
}
}
Class “Work” inherits from “IWork”.
Type type = Type.GetType("A");
MethodInfo MI = type.GetMethod("B");
object X = MI.Invoke(Activator.CreateInstance(type), new object[] { new Work() });
The above line throws argument error..”Expected IWork…instead of Work..”
If I write
object X = MI.Invoke(Activator.CreateInstance(type), new object[] { (new Work() as IWork) });
it works.
I want to know why it is not able to infer and typecast automatically polymorphism or have I got it all wrong? Can we have a custom Binder class to do the plumbing?
Please help.
EDIT
OK. Sorry guys for concealing the real problem. It works for above..also works with Array[] but throws error on List<>…
So my class would be:
public class A
{
public string B(List<IWork> WorkToDo)
{
return WorkToDo.ToString();
}
}
Type type = Type.GetType("A");
MethodInfo MI = type.GetMethod("B");
List<Work> WL = new List<Work>(); WL.Add(new Work());
object X = MI.Invoke(Activator.CreateInstance(type), new object[] { WL });
Error is:
Object of type ‘System.Collections.Generic.List
1[Work]' cannot be1[IWork]’.
converted to type 'System.Collections.Generic.List
With your update, the problem becomes obvious.
List<Work>is not aList<IWork>.List<T>is not covariant, as givenThe method accepting
List<IWork>could very well attempt to addnew OtherWork(), and that would be a perfectly legal compile time thing to do. If the method was passed aList<Work>, adding OtherWork would be a completely illegal runtime thing to do, and thus the language prevents you from passingList<Work>whereList<IWork>is expected.What you might want to do is have the method accept
IEnumerable<IWork>, which would allow you to pass yourList<Work>.IEnumerable<T>is covariant (in C# 4), and it can be because it is readonly, there would be no writes or adds to the sequence.Full working example:
As for why it works for array, array variance is broken. It allows things at compile time that could be completely dangerous at runtime. The same issue could be present at runtime, if you have a method accepting
IWork[] array, and you passWork[], the compiler will allow you to do it in this case. However, it would be perfectly legal (at compile time) to sayarray[0] = new OtherWork();, and you would get a runtime exception.