Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a nice way to avoid using reflection to populate my virtual ListView?

I have a ListView in virtual mode, and the underlying data is being stored in a List<MyRowObject>. Each column of the ListView corresponds to a public string property of MyRowObject. The columns of my ListView are configurable during runtime, such that any of them can be disabled and they can be reordered. To return a ListViewItem for the RetrieveVirtualItem event, I have a method similar to:

class MyRowObject
{
    public string[] GetItems(List<PropertyInfo> properties)
    {
        string[] arr = new string[properties.Count];
        foreach(PropertyInfo property in properties)
        {
            arr[i] = (string)property.GetValue(this,null);
        }
        return arr;
    }
}

The event handler for RetrieveVirtualItem looks similar to:

private void listView_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
    e.Item = new ListViewItem(_virtualList[e.ItemIndex].GetItems(_currentColumns));
}

Maybe not surprisingly, benchmarking shows that this method is significantly slower than an implementation that accessed properties directly in a hardcoded order, and the slowdown is just significant enough that I would like to find a better solution.

The most promising idea I've had is to use an anonymous delegate to tell the MyRowObject class how to directly access the properties, but if it's possible I couldn't get the semantics right (given the name of a property stored in a string, is there a way I can write a closure to directly access that property?).

So, is there a nice way to avoid using reflection to populate my ListView without losing any functionality?

The open source extension of ListView is off limit because of company policy.

like image 775
jtabak Avatar asked Oct 13 '22 21:10

jtabak


1 Answers

You could use these 2 functions

    private List<Func<T, string>> BuildItemGetters<T>(IEnumerable<PropertyInfo> properties)
    {
        List<Func<T, string>> getters = new List<Func<T, string>>();
        foreach (var prop in properties)
        {
            var paramExp = Expression.Parameter(typeof(T), "p");

            Expression propExp = Expression.Property(paramExp, prop);
            if (prop.PropertyType != typeof(string))
                propExp = Expression.Call(propExp, toString);

            var lambdaExp = Expression.Lambda<Func<T, string>>(propExp, paramExp);

            getters.Add(lambdaExp.Compile());
        }

        return getters;
    }

    private string[] GetItems<T>(List<Func<T, string>> properties, T obj)
    {
        int count = properties.Count;
        string[] output = new string[count];

        for (int i = 0; i < count; i++)
            output[i] = properties[i](obj);

        return output;
    }

Call the BuildItemGetters (sorry for the name, couldn't think of anything ;) once with a list of properties you want to get from the rows. Then just call the GetItems for each row. Where obj is the row and the list is the one you got from the other function.

For T just use the class name of your Row, like:

var props = BuildItemGetters<MyRowObject>(properties);
string[] items = GetItems(props, row);

ofcourse, only call the build when the columns change

like image 80
Doggett Avatar answered Nov 15 '22 09:11

Doggett