Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use reflection to simplify constructors & comparisons?

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?

like image 341
Cogwheel Avatar asked Aug 27 '09 14:08

Cogwheel


1 Answers

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);
}
like image 95
grenade Avatar answered Oct 26 '22 00:10

grenade