Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The advantage of obtaining the PropertyInfo via dissecting an expression is that it gives you compile-time checking and provides better refactoring support.</p> <p>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.</p> <p>If you know which exact property you will need to access in advance, expressions are the way to go.</p> <p>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.</p> <p>Here's an example of using expressions to perform grid column formatting with your very own PropertyHelper Class.</p> <p>Jump to GridForm.FormatGrid() to view the important bits.</p> <pre><code>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 =&gt; e.ID).Visible = false; this.GridColumn(e =&gt; e.Text).DefaultCellStyle = redCellStyle; this.GridColumn(e =&gt; e.Money).DefaultCellStyle = moneyCellStyle; } private DataGridViewColumn GridColumn&lt;TProperty&gt;(Expression&lt;Func&lt;TestEntity, TProperty&gt;&gt; expr) { var propInfo = PropertyHelper&lt;TestEntity&gt;.GetProperty(expr); var column = this.DataGridView.Columns[propInfo.Name]; return column; } } public static class PropertyHelper&lt;T&gt; { public static PropertyInfo GetProperty&lt;TValue&gt;( Expression&lt;Func&lt;T, TValue&gt;&gt; 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&lt;TestEntity&gt; GetTestData() { var entities = new List&lt;TestEntity&gt;(); for (var i = 1; i &lt;= 10; i++) { var testEntity = new TestEntity { ID = i, Text = "Entity " + i.ToString(), Money = i * 100m }; entities.Add(testEntity); } return entities; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload