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 3870618
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:44:42+00:00 2026-05-19T21:44:42+00:00

I want to add an item to a Generic list using reflection. In the

  • 0

I want to add an item to a Generic list using reflection. In the method “DoSomething”, I am trying to finish the following line,

pi.PropertyType.GetMethod("Add").Invoke(??????)

but I am getting different kinds of error.

Below is my complete code

public class MyBaseClass
{        
    public int VechicleId { get; set; }        
}    
public class Car:MyBaseClass
{
    public string Make { get; set; }
}    
public class Bike : MyBaseClass
{
    public int CC { get; set; }
}        
public class Main 
{
    public string AgencyName { get; set; }
    public MyBaseCollection<Car> lstCar {get;set;}

    public void DoSomething()
    {
        PropertyInfo[] p =this.GetType().GetProperties();
        foreach (PropertyInfo pi in p)
        {
            if (pi.PropertyType.Name.Contains("MyBaseCollection"))
            {
                //Cln contains List<Car>
                IEnumerable<MyBaseClass> cln = pi.GetValue(this, null) as IEnumerable<MyBaseClass>;

                **//Now using reflection i want to add  a new car to my object this.MyBaseCollection**
                pi.PropertyType.GetMethod("Add").Invoke(??????)
            }
        }    
    }
}

Any ideas / suggestion ?

  • 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-19T21:44:42+00:00Added an answer on May 19, 2026 at 9:44 pm

    I think you want:

    // Cast to IEnumerable<MyBaseClass> isn't helping you, so why bother?
    object cln = pi.GetValue(this, null);
    
    // Create myBaseClassInstance. 
    // (How will you do this though, if you don't know the element-type?)
    MyBaseClass myBaseClassInstance = ...
    
    // Invoke Add method on 'cln', passing 'myBaseClassInstance' as the only argument.
    pi.PropertyType.GetMethod("Add").Invoke(cln, new[] { myBaseClassInstance } );
    

    Since you don’t know what the element-type of the collection is going to be (could be Car, Bike, Cycle etc.) you’re going to find it hard to find a useful cast. For example, although you say the collection will definitely implement IList<SomeMyBaseClassSubType>, that isn’t all that helpful since IList<T> isn’t covariant. Of course, casting to IEnumerable<MyBaseClass> should succeed, but that won’t help you since it doesn’t support mutations. On the other hand, if your collection-type implemented the non-generic IList or ICollection types, casting to those might come in handy.

    But if you’re sure that the collection will implement IList<Car> (i.e. you know the element-type of the collection beforehand), things are easier:

    // A much more useful cast.
    IList<Car> cln = (IList<Car>)pi.GetValue(this, null);
    
    // Create car.
    Car car = ...
    
    // The cast helped!
    cln.Add(car);
    
    • 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 generic method: class Foo attr_reader def add(object) item
I want to permanently add an item to the cache. I am using the
I want to add an event to the List generic class in C# in
I want to add new items to my generic list when user clicks on
I have a menu item to which I add another item. Now I want
I have a list of string values that I want add to a hashtable
I used RollingFileAppender. And I want add a blank line to the log when
I have such generic class of list: using System; using System.Collections; using System.Collections.Generic; using
I have the following code, where i am trying to create a generic collection
I'm having some trouble with a generic method I'm writing. It has the following

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.