Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6988703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:06:58+00:00 2026-05-27T19:06:58+00:00

I am trying to call a class method through reflection. public class A {

  • 0

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.List
1[IWork]’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:06:59+00:00Added an answer on May 27, 2026 at 7:06 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a call to a Parent class method from a
I am trying to call the browse() method of the FileReference class from JavaScript
I'm trying to intercept a method call (afterInsert() of a domain class) in a
I am trying to intercept a method through a proxy class and am getting
I am trying the following: public partial class PopUps : System.Web.UI.Page { public string
I'm trying to access to a class variable through an instance method through an
I'm trying to call my good old 'RegisterClientScriptBlock' on the friendly 'ScriptManager' class. It
I'm trying to call Error(My Test) in normal cpp class (Not a COM object,
I'm trying to modify the class of an element if an ajax call based
I'm trying to add a call to the StaticClassName.class field access to an existing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.