Advertisement
Dennisaa

ObjectExtensions

May 3rd, 2015
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. namespace Microsoft.Services.TestTools.UITesting.Html {
  2.     using System;
  3.     using System.Reflection;
  4.  
  5.     /// <summary>
  6.     /// <see cref="Object"/> extension methods
  7.     /// </summary>
  8.     public static class ObjectExtensions {
  9.  
  10.         /// <summary>
  11.         /// Executes the provided <see cref="Action"/> for each public property on the object.
  12.         /// It passes the name and the value of the property to the action.
  13.         /// </summary>
  14.         /// <param name="self">The self.</param>
  15.         /// <param name="action">The action.</param>
  16.         public static void ForEachProperty(this object self, Action<string, string> action) {
  17.             var props = self.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
  18.             foreach (var prop in props) {
  19.                 var val = prop.GetValue(self, null).ToString();
  20.                 action(prop.Name, val);
  21.             }
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement