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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:07:43+00:00 2026-06-10T22:07:43+00:00

I have the name & type of an object and the method name. Ideally

  • 0

I have the name & type of an object and the method name.
Ideally I would like to instantiate the object & call the method. All is well with that, but I am getting an error trying to process the results.

private void GetData(DropDownList ddl)
    {
        ObjectDataSource ods = (ObjectDataSource)ddl.DataSourceObject;
        System.Reflection.Assembly assembly = typeof(ExistingObject).Assembly;

        Type type = assembly.GetType(ods.SelectMethod);
        var instance = Activator.CreateInstance(type);

        IterateCollection(instance, ddl);
    }

    private static void IterateCollection<T>(T instance, DropDownList ddl) 
    {
        ObjectDataSource ods = (ObjectDataSource)ddl.DataSourceObject;
        Type type = instance.GetType();
        MethodInfo methodInfo = type.GetMethod(ods.SelectMethod);

        LinkedList<T> col = (LinkedList<T>)methodInfo.Invoke(null, new object[] { (T)instance });

        foreach (T o in col)
        {
            string textfield = Convert.ToString(GetPropValue(o, ddl.DataTextField));
            string valuefield = Convert.ToString(GetPropValue(o, ddl.DataValueField));
            ListItem li = new ListItem(textfield, valuefield);
            if ((bool?)GetPropValue(o, "Active") != true)
                li.Attributes.CssStyle.Add("background-color", "gray");
            ddl.Items.Add(li);
        }
    }

I am getting an error on the “Invoke” line saying
System.InvalidCastException :
Unable to cast object of type 'System.Collections.Generic.LinkedList``1[Business.Objects.MyType]' to type 'System.Collections.Generic.ICollection
1[System.Object]’.

I would like to be able to iterate through the collection. How can I do this?
How can I create a LinkedList or similar?

Thanks

  • 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-06-10T22:07:45+00:00Added an answer on June 10, 2026 at 10:07 pm

    It appears that the type T must always provide a method with the name stored in ods.SelectMethod and multiple properties. If T has to implement an interface containing this method, you can constrain IterateCollection<T> to that interface using:

       private static void IterateCollection<T>(T instance, DropDownList ddl)
           where T: IMyInterface 
    

    Assuming you cannot do that, use a LinkedList<object>. You are invoking the methods using reflection so generics will not give you any syntactic assistance.

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

Sidebar

Related Questions

Suppose I have a class ObjectInfo which contains Object name & Object type as
I would like to have declaration like this: void Date::get_days_name(const Date& = this) which
I have this virtual method: const string& my_class::to_string() const { string str(this->name + string(
I have filled List<Object1> Object 1 contain 2 fields (int id & string name)
Have this upload script, and it works. But I would like to add a
I have a group name which contains special characters. CN=IN&T DC Gebnn/Dohn,OU=ABGroups,OU=Hammers,DC=MyCompany,DC=int I am
In my code i have the following: optionlist.AppendFormat(<li> <a href=\setlocale.aspx?returnURL=Default.aspx&localesetting={0}\>{1}</a> </li>, DB.RSField(rs, Name), DB.RSField(rs,
I have a query that selects data into a custom type- UserData curData =
i have a class called light that loads its state by ajax: function Light(name,
I have a form that is displayed, not by ShowDialog, but by setting its

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.