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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:56:31+00:00 2026-05-26T05:56:31+00:00

I am using dynamic objects for my view models, as I find the overhead

  • 0

I am using dynamic objects for my view models, as I find the overhead from using something like Automapper unnecessary and find this approach a lot more flexible and lightweight. I am using the builder from impromptu-interface like this:

private dynamic New = Builder.New();

private dynamic GetViewModel(Product p)
{
    var viewModel = New.Product( id : p.Id, name : p.Name );
    viewModel.AdditionalProperty = "some additional data";
    return viewModel;
}

There are a few scenarios where “expanding” the actual object would be better then remapping all the properties one by one, similar to how you would do in JavaScript using jQuery.extend()

private dynamic GetViewModel(Product p)
{
    var viewModel = //create base dynamic object, that has all the members of p.
    viewModel.AdditionalProperty = "some additional data";
    return viewModel;
}

This should be achievable using ExpandoObject combined with reflection and iterating through all the members, but I would like to know if there’s a cleaner/neater solution.

  • 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-26T05:56:31+00:00Added an answer on May 26, 2026 at 5:56 am

    I ended up implementing it like this:

    public class ExpandedObject : DynamicObject
    {
        private readonly IDictionary<string, object> expando = new ExpandoObject();
    
        public ExpandedObject(object o)
        {            
            foreach (var propertyInfo in o.GetType().GetProperties(BindingFlags.Public|BindingFlags.Instance))
            {
                this.expando[propertyInfo.Name] = Impromptu.InvokeGet(o, propertyInfo.Name);
            }
        }
    
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {            
            return this.expando.TryGetValue(binder.Name, out result);
        }
    
        public override bool  TrySetMember(SetMemberBinder binder, object value)
        {
            this.expando[binder.Name] = value;
            return true;
        }
    }
    

    and the tests:

    [TestFixture]
    public class ExpandedObjectTest
    {
        [Test]
        public void Can_add_new_properties_to_expanded_object()
        {
            dynamic expanded = new ExpandedObject(new object());
            var data = "some additional data";
            expanded.data = data;
            Assert.AreEqual(data, expanded.data);
        }
    
        [Test]
        public void Copies_existing_properties()
        {            
            var obj = new { id = 5 };            
            dynamic expanded = new ExpandedObject(obj);            
            Assert.AreEqual(obj.id, expanded.id);            
        }
    }
    

    This makes use of Impromptu.InvokeGet() instead of PropertyInfo.GetValue() because Impromptu.InvokeGet() uses the DLR and as such about 2.5x faster than using than reflection from my tests. Overall this works reasonably fast and the overhead for upto 10,000 objects is almost nonexistant.

    I should note that this won’t work to expand other ExpandoObject or similar, but this should not really be necessary anyway.

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

Sidebar

Related Questions

Consider this url: www.anysite.com/get_count/?sex=5&sex=6&city=5&city=7&job=7&job=8...... For using in dynamic query in view: model = anyModel.objects.filter(sex=5).filter(sex=6).filter(city=5)....
How can dynamic Key-value pairs of objects be stored in app.config in using the
I would like to write a hql query using a dynamic instantiation with a
This is pretty much a duplicate question but instead of using Castle Dynamic Proxy
I'm having a problem using nested partials with dynamic form builder code (from the
I'm wanting to have a simple duck typing example in C# using dynamic objects.
When Using Dynamic Proxies, how do I access the underlying object's Annotations? Specifically I'm
I am trying to create a new contact using Dynamic Entity. The sample i
I have a table with almost 800,000 records and I am currently using dynamic
SO has plenty of Q&As about how to accomplish various tasks using dynamic SQL,

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.