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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:17:06+00:00 2026-05-25T16:17:06+00:00

In C#, I would like to figure out if it’s possible to declare an

  • 0

In C#, I would like to figure out if it’s possible to declare an anonymous type where the fields are not known until run-time.

For example, if I have a List of key/value pairs, can I declare an anonymous type based on the contents of that list? The specific case I’m working with is passing parameters to Dapper, where I don’t know ahead of time how many parameters I will have.

List<Tuple<string, string>> paramList = new List<Tuple<string, string>>() {
    new Tuple<string, string>("key1", "value1"),
    new Tuple<string, string>("key2", "value2")
    ...
};

I’d like to convert this List (or an equivalent Map) into an anonymous type that I can pass to Dapper as query parameters. So ideally, the above list would wind up looking like this, if defined as an anonymous type:

new { key1=value1, key2=value2, ... }

I’ve seen several questions on StackOverflow asking about extending anonymous types after they are declared (“extendo objects”), or declaring arbitrary fields on an object after it’s created, but I don’t need to do that… I just need to declare the types dynamically up-front once. My suspicion is that it will require some fancy reflection, if it’s possible at all.

My understanding is that the compiler defines a type for anonymous classes under the hood at compile-time, so if the fields of that class are not available until run-time, I might be out of luck. My use case may in fact be no different in actuality than using an “extendo object” to define arbitrary fields, whenever.

Alternatively, if anyone knows of a better way to pass query parameters to Dapper (rather than declaring an anonymous class), I would love to hear about that as well.

Thanks!

UPDATE

Sorry for the delay in getting back to this one! These answers were all great, I wish I could give points to everyone. I ended up using jbtule’s solution (with edit by Sam Saffron), passing IDynamicParameters to Dapper, so I felt I had to give the answer to him. The other answers were also good, and answered specific questions that I had asked. I really appreciate everyone’s time on this!

  • 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-25T16:17:07+00:00Added an answer on May 25, 2026 at 4:17 pm

    Dapper’s creators were very aware of this problem. This kind of functionality is really needed for INSERT and UPDATE helpers.

    The Query, Execute and QueryMultiple methods take in a dynamic parameter. This can either be an anonymous type, a concrete type or an object that implements IDynamicParameters.

    public interface IDynamicParameters
    {
        void AddParameters(IDbCommand command, Identity identity);
    }
    

    This interface is very handy, AddParameters is called just before running any SQL. Not only does this give you rich control over the parameters sent to SQL. It allows you to hook up DB specific DbParameters, since you have access to the command (you can cast it to the db specific one). This allows for support of Table Values Parameters and so on.

    Dapper contains an implementation of this interface that can be used for your purposes called DynamicParameters. This allows you to both concatenated anonymous parameter bags and add specific values.

    You can use the method AddDynamicParams to append an anonymous type.

    var p = new DynamicParameters();
    p.AddDynamicParams(new{a = "1"});
    p.AddDynamicParams(new{b = "2", c = "3"});
    p.Add("d", "4")
    var r = cnn.Query("select @a a, @b b, @c c, @d d", p);
    // r.a == 1, r.b == 2, r.c == 3, r.d == 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a TDD newb and I would like to figure out how to
I know almost nothing about cryptography, but I would like to figure out how
I would like a .htaccess rewrite rule, but I can't figure out how to
I couldn't figure out that from SASS documentation. For example I would like to
I would like to figure out where everything is in django admin. Since i
I have a (3,3) numpy array and would like to figure out the sign
I have just created a java tic-tac-toe game i would like to figure out
I am using the XtraNavBar from DevExpress and I would like to figure out
I would like to figure out how to select a table where the value
I would like to figure out how to save a bitmap or vector graphics

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.