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

  • Home
  • SEARCH
  • 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 7185651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:27:43+00:00 2026-05-28T18:27:43+00:00

How would I get change this snippet to correctly add an instance of A

  • 0

How would I get change this snippet to correctly add an instance of A to a List<A>, B to a List<B>, etc.?

// someChild's actual type is A
object someChild = GetObject();
// collection's actual type is List<A> though method below returns object
dynamic list = GetListFromSomewhere(...);
// code below throws a RuntimeBinderException
list.Add(somechild);

The exception is thrown because, while the Add() is found by the binder, it passes in dynamic which fails overload resolution. I prefer not to change the above to use reflection, or at least to minimize that. I do have access to the instance of System.Type for each of A and List<A>. The class or method containing the above code is itself not generic.

  • 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-28T18:27:44+00:00Added an answer on May 28, 2026 at 6:27 pm

    All you need is to make the binding for the argument dynamic too – so you just need the type of someChild to be dynamic:

    dynamic someChild = GetObject();
    dynamic list = GetListFromSomewhere(...);
    list.Add(somechild);
    

    In your previous code, the compiler would have remembered that the compile-time type of someChild was object, and so used that compile-time type instead of the execution-time type. The execution-time binder is smart at only treating dynamic expressions dynamically for overload resolution.

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

Sidebar

Related Questions

Following is how you would get the current solution directory from an add-in: _applicationObject
Can someone please shed some light on how I would get this to work:
I've been using this little snippet to select random images. However I would like
I have this code snippet where we get a collection from COM Dll public
I am using ManagementClass.GetStronglyTypedClassCode to get a reference to CodeTypeDeclaration instance. I would like
I have a snippet of code like this: std::list<boost::shared_ptr<Point> > left, right; // ...
I am having a problem trying to get this snippet of code to output
i would get details from two MySQL tables tables structure as shown table:App |AppID|AppName|AppType|
How would get find an average from an array? If I have the array:
When running the Microsoft Application Verifier i would get an error 0202 on shutdown:

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.