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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:10:01+00:00 2026-05-16T14:10:01+00:00

I have an object, and before I serialize it to Json, I’d like to

  • 0

I have an object, and before I serialize it to Json, I’d like to add a property to it. So my object will look like this:

    public class MySpecialObject {
       public int Id { get; set; }
       public string OneProperty { get; set; }
       public string ASecondProperty { get; set; }
    }

However, much later in the program I often want to add a property or two. This usually occurs in my controller, right before converting it to Json. I’d like to do something like this:

    ///Retrieve object from database
    var MySpecialObject = specialObjectRepository.Get(id);

    MySpecialObject.["DynamicProperty"] = "Dynamic Property!";

    ///Pretend I do Json serialization here
    return MySpecialObject;

That’s obviously made up, but I hope what I’m trying to do is clear. In Javascript this is really easy to do since it is loosely typed. I thought with the dynamic keyword in C# 4.0 this would be possible, but it looks like that’s not what it is for (though I could be completely missing the obvious). I guess I could create a class for these kinds of situations, but it’d be easier to just do it in one line. I know this sounds lazy, but it feels like what I’m trying to do should be possible.

  • 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-16T14:10:02+00:00Added an answer on May 16, 2026 at 2:10 pm

    You have to use the new ExpandoObject class:

        public class MySpecialObject:ExpandoObject {
           public int Id { get; set; }
           public string OneProperty { get; set; }
           public string ASecondProperty { get; set; }
        }
    

    and, before serialization,simply write:

    var MySpecialObject = specialObjectRepository.Get(id);
    
    MySpecialObject.DynamicProperty = "Dynamic Property!";
    

    You are not limited to add new properties, you can also add new methods and new events.

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

Sidebar

Related Questions

I have a webapi public ISearchProviderCommandResult ExecuteCommand(ISearchProviderCommand searchCommand) { //serialize the object before sending
I have a class that looks like this public class SomeClass { public SomeChildClass[]
Is there anything special I have to do to a JSON object before I
I have the following object: [Serializable] public class ExampleImage { public int ID {
I have a class that inherits from ApiController. It has a Put-method like this:
I have an object that maintains a property bag of various properties. This is
I have this object: stdClass Object ( [daily_inventoryID] => 1 [inventory_timestamp] => 2012-06-08 14:35:42
I have 2 Lists: List<User> List<UserStats> So I want to add a property Count
I have a simple object I'm trying to serialize using DataContractSerializer. In my unit
I have many JavaScript objects in my application, something like: function Person(age) { this.age

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.