简体   繁体   中英

what's the advantage of Property Info?

In .net FrameWork 3.5 we can get the Property Information using below mentioned code.

using System;
using System.Linq.Expressions;
using System.Reflection;

class Foo
{
    public string Bar { get; set; }
}
static class Program
{
    static void Main()
    {
        PropertyInfo prop = PropertyHelper<Foo>.GetProperty(x => x.Bar);
    }
}
public static class PropertyHelper<T>
{
    public static PropertyInfo GetProperty<TValue>(
        Expression<Func<T, TValue>> selector)
    {
        Expression body = selector;
        if (body is LambdaExpression)
        {
            body = ((LambdaExpression)body).Body;
        }
        switch (body.NodeType)
        {
            case ExpressionType.MemberAccess:
                return (PropertyInfo)((MemberExpression)body).Member;
            default:
                throw new InvalidOperationException();
        }
    }
}

This can also be done by creating the instance of a class and access the Property Member. So what's the advantage of Property Info?

PropertyInfo is used to get information of properties of class. Creating an instance is not needed. Advantage is that it removes possibility of typing error.

Expressions are entirely different concept (that uses Reflection internally). Expressions are used to represent method body as tree structure. This allows flexibility for creating/tweaking method definition at runtime.

This capability of Expressions is harnessed by Queryable class to build/execute dynamic queries at remote source.

Example, Consider INotifyPropertyChanged interface. It is used for property change notification.

Usual implementation takes property name as string parameter. Thus typing error are detected at runtime. Also Refactoring can break the code (Though Smart refactor tool takes care of this).

    void RaisePropertyChanged(PropertyChangedEventArgs args)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }

    public string Name
    {
        set
        {
            _name = value;
            RaisePropertyChanged("Name"); // Property name is specified as string
        }
    }

A better implementation (not the performance efficient though) takes property name as Expression .

    void RaisePropertyChanged<T>(Expression<Func<T>> selectorExpression)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            MemberExpression body = selectorExpression.Body as MemberExpression;

            handler(this, new PropertyChangedEventArgs(body.Member.Name));
        }
    }

    public string Name
    {
        set
        {
            _name = value;
            RaisePropertyChanged( () => this.Name); // Property is specified instead of name that removes typing error
        }
    }

The advantage of obtaining the PropertyInfo via dissecting an expression is that it gives you compile-time checking and provides better refactoring support.

If you change the name of the property from Bar to Barr, for instance, your code will no longer compile thus allowing you to catch invalid member access bugs without actually running your application.

If you know which exact property you will need to access in advance, expressions are the way to go.

I've found expressions to be especially helpful in data binding scenarios where you need to specify the name of the property to be bound to a grid column or a list control for example. Using expressions in this type of scenario keeps the maintenance costs right down.

Here's an example of using expressions to perform grid column formatting with your very own PropertyHelper Class.

Jump to GridForm.FormatGrid() to view the important bits.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq.Expressions;
using System.Reflection;
using System.Windows.Forms;

namespace ExpressionSample
{
    public class TestEntity
    {
        public int ID { get; set; }
        public string Text { get; set; }
        public decimal Money { get; set; }
    }

    public partial class GridForm : Form
    {
        public GridForm()
        {
            this.InitializeComponent();
        }

        private void GridForm_Load(object sender, EventArgs e)
        {
            this.FillGrid();
            this.FormatGrid();
        }

        private void FillGrid()
        {
            this.DataGridView.DataSource = TestDataProducer.GetTestData();
        }

        private void FormatGrid()
        {
            var redCellStyle = new DataGridViewCellStyle() { ForeColor = Color.Red };
            var moneyCellStyle = new DataGridViewCellStyle() { Format = "$###,###,##0.00" };

            this.GridColumn(e => e.ID).Visible = false;
            this.GridColumn(e => e.Text).DefaultCellStyle = redCellStyle;
            this.GridColumn(e => e.Money).DefaultCellStyle = moneyCellStyle;
        }

        private DataGridViewColumn GridColumn<TProperty>(Expression<Func<TestEntity, TProperty>> expr)
        {
            var propInfo = PropertyHelper<TestEntity>.GetProperty(expr);
            var column = this.DataGridView.Columns[propInfo.Name];

            return column;
        }
    }

    public static class PropertyHelper<T>
    {
        public static PropertyInfo GetProperty<TValue>(
            Expression<Func<T, TValue>> selector)
        {
            Expression body = selector;
            if (body is LambdaExpression)
            {
                body = ((LambdaExpression)body).Body;
            }
            switch (body.NodeType)
            {
                case ExpressionType.MemberAccess:
                    return (PropertyInfo)((MemberExpression)body).Member;
                default:
                    throw new InvalidOperationException();
            }
        }
    }

    public static class TestDataProducer
    {
        public static IList<TestEntity> GetTestData()
        {
            var entities = new List<TestEntity>();

            for (var i = 1; i <= 10; i++)
            {
                var testEntity = new TestEntity {
                    ID = i,
                    Text = "Entity " + i.ToString(),
                    Money = i * 100m
                };

                entities.Add(testEntity);
            }

            return entities;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM