开发者

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)
    {
        s开发者_JAVA百科tring[] 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.


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


BindingSource and PropertyDescriptor are more elegant techniques for performing manual data-binding, which is more-or-less what you're doing with the ListView when it's in VirtualMode. Although it generally uses reflection internally anyway, you can rely on it to work efficiently and seamlessly.

I wrote a blog article recently which explains in detail how to use these mechanisms (although it's in a different context, the principles are the same) - http://www.brad-smith.info/blog/archives/104


Take a look at Reflection.Emit. With this, you can generate code on the fly that accesses a specific property. This CodeProject article has an interesting description of the mechanism: http://www.codeproject.com/KB/cs/fast_dynamic_properties.aspx.

I haven't throughly reviewed the code of the project, but my first impression is that the basic idea looks promising. However, one of the improvements I would make is that some of the pieces of the class should be static and shared, for example InitTypes and the created dynamic assembly. For the rest, it looks like it fits what you're looking for.


I don't know enough about c# to tell you if that is possible, but I'll go and hack my way with something like this:

  • once, i will try to get 'delegate pointers' for every member that I need, and will do that through reflection - if it were c++, those pointers would be vtable offsets for property getter function
  • will create a map with string->pointer offset
  • will use the map to call the getter function directly through the pointer.

Yes, it seems like a magic, but i guess that someone with enough CLR/MSIL knowledge can shed the light on that if it is remotely possible.


Here is another variant caching the get methods for each property.

    public class PropertyWrapper<T>
    {
        private Dictionary<string, MethodBase> _getters = new Dictionary<string, MethodBase>();

        public PropertyWrapper()
        {
            foreach (var item in typeof(T).GetProperties())
            {
                if (!item.CanRead)
                    continue;

                _getters.Add(item.Name, item.GetGetMethod());
            }
        }

        public string GetValue(T instance, string name)
        {
            MethodBase getter;
            if (_getters.TryGetValue(name, out getter))
                return getter.Invoke(instance, null).ToString();

            return string.Empty;
        }
    }

to get a property value:

var wrapper = new PropertyWrapper<MyObject>(); //keep it as a member variable in your form

var myObject = new MyObject{LastName = "Arne");
var value = wrapper.GetValue(myObject, "LastName");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜