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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:33:55+00:00 2026-05-12T11:33:55+00:00

I hate having a bunch of left/right methods. Every time a property is added

  • 0

I hate having a bunch of “left/right” methods. Every time a property is added or removed, I have to fix up each method. And the code itself just looks … wrong.

public Foo(Foo other)
{
    this.Bar = other.Bar;
    this.Baz = other.Baz;
    this.Lur = other.Lur;
    this.Qux = other.Qux;
    this.Xyzzy= other.Xyzzy;
}

Really this is just an unrolled loop that iterates through the properties, copying them between objects. So why not be honest about that fact? Reflection to the rescue!

public Foo(IFoo other)
{
    foreach (var property in typeof(IFoo).GetProperties())
    {
        property.SetValue(this, property.GetValue(other, null), null);
    }
}

I may just be trying to force a paradigm I learned from Lua onto C#, but this particular example doesn’t seem too smelly to me. From here, I started to do some more complex things that were sensitive to the order of the fields. For example, rather than having a stack of virtually identical if statements to compose a string from the fields, I just iterate over them in the desired order:

public override string ToString()
{
    var toJoin = new List<string>();
    foreach (var property in tostringFields)
    {
        object value = property.GetValue(this, null);
        if (value != null)
            toJoin.Add(value.ToString());
    }
    return string.Join(" ", toJoin.ToArray());
}
private static readonly PropertyInfo[] tostringFields =
{
    typeof(IFoo).GetProperty("Bar"),
    typeof(IFoo).GetProperty("Baz"),
    typeof(IFoo).GetProperty("Lur"),
    typeof(IFoo).GetProperty("Qux"),
    typeof(IFoo).GetProperty("Xyzzy"),
};

So now I have the iterability I wanted, but I still have stacks of code mirroring each property I’m interested in (I’m also doing this for CompareTo, using a different set of properties in a different order). Worse than that is the loss of strong typing. This is really starting to smell.

Well what about using attributes on each property to define the order? I started down this road and indeed it worked well, but it just made the whole thing look bloated. It works great semantically, but I’m always wary of using advanced features just because they’re “neat.” Is using reflection in this way overkill? Is there some other solution to the left/right code problem I’m missing?

  • 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-12T11:33:55+00:00Added an answer on May 12, 2026 at 11:33 am

    Using reflection in and of itself is not bad, but you will take a performance hit especially if you do it recursively.

    I am not a fan of the hard coded copy constructors either because developers forget to update them when they add new properties to a class.

    There are other ways of accomplishing what you want, including Marc Gravells Hyper Property Descriptor or if you want to learn some IL and OPCodes you can use System.Reflection.Emit or even Cecil from Mono.

    Here’s an example of using Hyper Property Descriptor that you can possibly tailor to your needs:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using Hyper.ComponentModel;
    namespace Test {
        class Person {
            public int Id { get; set; }
            public string Name { get; set; }
        }
        class Program {
            static void Main() {
                HyperTypeDescriptionProvider.Add(typeof(Person));
                var properties = new Dictionary<string, object> { { "Id", 10 }, { "Name", "Fred Flintstone" } };
                Person person = new Person();
                DynamicUpdate(person, properties);
                Console.WriteLine("Id: {0}; Name: {1}", person.Id, person.Name);
                Console.ReadKey();
            }
            public static void DynamicUpdate<T>(T entity, Dictionary<string, object>  {
                foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(typeof(T)))
                    if (properties.ContainsKey(propertyDescriptor.Name))
                        propertyDescriptor.SetValue(entity, properties[propertyDescriptor.Name]);
            }
        }
    }
    

    If you decide to carry on using reflection, you can reduce the performance hit by caching your calls to GetProperties() like so:

    public Foo(IFoo other) {
        foreach (var property in MyCacheProvider.GetProperties<IFoo>())
            property.SetValue(this, property.GetValue(other, null), null);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 278k
  • Answers 278k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I see if-expressions in mathematical notation fairly often. The usual… May 13, 2026 at 3:10 pm
  • Editorial Team
    Editorial Team added an answer If your system is in-house and/or you have a limited… May 13, 2026 at 3:10 pm
  • Editorial Team
    Editorial Team added an answer I couldn't quite decipher what your setup is, but you… May 13, 2026 at 3:10 pm

Related Questions

I am wondering if there is a way to make ASP.NET controls play nicely
I have a bunch of pretty large CSV (comma separated values) files and I
My cell phone provider offers a limited number of free text messages on their
Re: http://www.codinghorror.com/blog/archives/000633.html Is there a plugin that turns VS2008's non-standard regex find/replace into a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.