Return to Snippet

Revision: 31759
at September 12, 2010 11:26 by jimfred


Updated Code
// [1] using PropertyOf
PropertyInfo pi1 = PropertyOf(() => Checkbox1.Checked);

// [2] Using Lambda to get a PropertyInfo from a CheckBox.Checked property.
// This is a variation of [1]
Expression<Func<bool>> expression = (() => Checkbox1.Checked);
PropertyInfo pi2 = ((expression.Body as MemberExpression).Member as PropertyInfo);

// [3] using PropertyHelper
PropertyInfo pi3 = PropertyHelper<CheckBox>.GetProperty(x => x.Checked);

// [4] using string name of property.
PropertyInfo pi4 = typeof(CheckBox).GetProperty("Checked");

// This proves that all 4 methods are itentical.
Debug.Assert(pi1 == pi2 && pi1 == pi3 && pi1 == pi4);

// This is how the PropertyInfo is used to Set (or Get) the value.
pi1.SetValue(Checkbox1, true, null);


/// <summary>
/// [1]
/// Given a lambda expression of a Property, return the PropertyInfo for the property.
/// http://codebetter.com/blogs/patricksmacchia/archive/2010/06/28/elegant-infoof-operators-in-c-read-info-of.aspx
/// </summary>
/// <typeparam name="T">Property type</typeparam>
/// <param name="expression">Lambda expression</param>
/// <returns>PropertyInfo for property</returns>
internal static PropertyInfo PropertyOf<T>(Expression<Func<T>> expression)
{
   MemberExpression body = (MemberExpression)expression.Body;
   return (PropertyInfo)body.Member;
}

      /// <summary>
      /// [3]
      /// From http://stackoverflow.com/questions/491429/how-to-get-the-propertyinfo-of-a-specific-property
      /// </summary>
      /// <typeparam name="T"></typeparam>
      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();
            }
         }
      }

Revision: 31758
at September 12, 2010 11:25 by jimfred


Updated Code
// [1] using PropertyOf
         PropertyInfo pi1 = PropertyOf(() => Checkbox1.Checked);

         // [2] Using Lambda to get a PropertyInfo from a CheckBox.Checked property.
         // This is a variation of [1]
         Expression<Func<bool>> expression = (() => Checkbox1.Checked);
         PropertyInfo pi2 = ((expression.Body as MemberExpression).Member as PropertyInfo);

         // [3] using PropertyHelper
         PropertyInfo pi3 = PropertyHelper<CheckBox>.GetProperty(x => x.Checked);

         // [4] using string name of property.
         PropertyInfo pi4 = typeof(CheckBox).GetProperty("Checked");

         // This proves that all 4 methods are itentical.
         Debug.Assert(pi1 == pi2 && pi1 == pi3 && pi1 == pi4);

         // This is how the PropertyInfo is used to Set (or Get) the value.
         pi1.SetValue(Checkbox1, true, null);


      /// <summary>
      /// [1]
      /// Given a lambda expression of a Property, return the PropertyInfo for the property.
      /// http://codebetter.com/blogs/patricksmacchia/archive/2010/06/28/elegant-infoof-operators-in-c-read-info-of.aspx
      /// </summary>
      /// <typeparam name="T">Property type</typeparam>
      /// <param name="expression">Lambda expression</param>
      /// <returns>PropertyInfo for property</returns>
      internal static PropertyInfo PropertyOf<T>(Expression<Func<T>> expression)
      {
         MemberExpression body = (MemberExpression)expression.Body;
         return (PropertyInfo)body.Member;
      }

      /// <summary>
      /// [3]
      /// From http://stackoverflow.com/questions/491429/how-to-get-the-propertyinfo-of-a-specific-property
      /// </summary>
      /// <typeparam name="T"></typeparam>
      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();
            }
         }
      }

Revision: 31757
at September 12, 2010 11:22 by jimfred


Updated Code
// [1] using PropertyOf
         PropertyInfo pi1 = PropertyOf(() => Checkbox1.Checked);

         // [2] Using Lambda to get a PropertyInfo from a CheckBox.Checked property.
         // This is a variation of [1]
         Expression<Func<bool>> expression = (() => Checkbox1.Checked);
         PropertyInfo pi2 = ((expression.Body as MemberExpression).Member as PropertyInfo);

         // [3] using PropertyHelper
         PropertyInfo pi3 = PropertyHelper<CheckBox>.GetProperty(x => x.Checked);

         // [4] using string name of property.
         PropertyInfo pi4 = typeof(CheckBox).GetProperty("Checked");

         Debug.Assert(pi1 == pi2 && pi1 == pi3 && pi1 == pi4);

         pi1.SetValue(Checkbox1, true, null);

      }

      /// <summary>
      /// [1]
      /// Given a lambda expression of a Property, return the PropertyInfo for the property.
      /// http://codebetter.com/blogs/patricksmacchia/archive/2010/06/28/elegant-infoof-operators-in-c-read-info-of.aspx
      /// </summary>
      /// <typeparam name="T">Property type</typeparam>
      /// <param name="expression">Lambda expression</param>
      /// <returns>PropertyInfo for property</returns>
      internal static PropertyInfo PropertyOf<T>(Expression<Func<T>> expression)
      {
         MemberExpression body = (MemberExpression)expression.Body;
         return (PropertyInfo)body.Member;
      }

      /// <summary>
      /// [3]
      /// From http://stackoverflow.com/questions/491429/how-to-get-the-propertyinfo-of-a-specific-property
      /// </summary>
      /// <typeparam name="T"></typeparam>
      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();
            }
         }
      }

Revision: 31756
at September 12, 2010 11:18 by jimfred


Initial Code
// [2] Using Lambda to get a PropertyInfo from a CheckBox.Checked property.
         // This is a variation of [1]
         Expression<Func<bool>> expression = (() => Checkbox1.Checked);
         PropertyInfo pi1 = ((expression.Body as MemberExpression).Member as PropertyInfo);

Initial URL

                                

Initial Description
It\'s not possible to get a reference to a property in C#. A reference would allow getting/setting a property inside another function or while enumerating a list. The infoof operator would work but it doesn\'t exist yet. This code shows several ways to get a PropertyInfo for a given property. #4 is the simplest but has the disadvantage that the string will not auto-rename.

Initial Title
C#, getting a reference to a property such as CheckBox.Checked

Initial Tags

                                

Initial Language
C#