سؤال

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.List1[Work]' cannot be converted to type 'System.Collections.Generic.List1[IWork]'.

هل كانت مفيدة؟

المحلول

With your update, the problem becomes obvious. List<Work> is not a List<IWork>. List<T> is not covariant, as given

interface IWork { }
class Work : IWork { } 
class OtherWork : IWork { } 

The method accepting List<IWork> could very well attempt to add new OtherWork(), and that would be a perfectly legal compile time thing to do. If the method was passed a List<Work>, adding OtherWork would be a completely illegal runtime thing to do, and thus the language prevents you from passing List<Work> where List<IWork> is expected.

What you might want to do is have the method accept IEnumerable<IWork>, which would allow you to pass your List<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:

namespace Foo
{
    public class A
    {
        // note the change to the signature
        public string B(IEnumerable<IWork> workToDo)
        {
            return workToDo.ToString();
        }

        static void Main()
        {
            var type = Type.GetType("Foo.A");
            var info = type.GetMethod("B");
            var list = new List<Work>();
            var x = info.Invoke(Activator.CreateInstance(type), new [] { list });
        }
    }

    public interface IWork { }
    public class Work : IWork { }
}

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 pass Work[], the compiler will allow you to do it in this case. However, it would be perfectly legal (at compile time) to say array[0] = new OtherWork();, and you would get a runtime exception.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top