title: How to iterate through all string properties and change their values using System.Reflection
body: <p>I need to loop through all <code>String</code> properties of an item, retrieve their values, and manipulate the values - without needing to know the name of the item or property itself.  So far, all the examples of <code>SetValue</code> that I've seen assume that you know the name of the property, and use that name to reference it.  In my case, I'm not calling anything by name, but rather simply iterating through every string.</p>
  <pre><code>public class SpecialClass
  {
      public string MyString1 { get; set; }
      public string MyString2 { get; set; }
  
      public void Sanitize()
      {
          Type CheckItemType = typeof(SpecialClass);
          Type strType = typeof(System.String);
          PropertyInfo[] propInfos = CheckItemType.GetProperties(
            BindingFlags.Public | BindingFlags.Instance);
  
          foreach (var propInfo in propInfos)
          {
              if (null != propInfo &amp;&amp; propInfo.CanWrite)
              {
                  if (propInfo.PropertyType == typeof(System.String))
                  {
                      // Retrieve the value of the string, manipulate, and then set it
  
                  }
              }
          }
      }
  }
  </code></pre>
  <p>In the above code, the <code>Sanitize</code> method would loop through the strings in the class and adjust their values if they meet certain conditions.</p>
  <p>While I found <a href="https://stackoverflow.com/questions/26382810/how-do-you-get-the-value-of-a-property-from-propertyinfo">this post</a>, what it describes seems to require that you know the name of the item and property.  In my case, I don't know either.  I have a Method whose purpose is to perform manipulation of all properties of type <code>String</code>.</p>
  <p>I can't do <a href="https://stackoverflow.com/questions/1196991/get-property-value-from-string-using-reflection">this</a> because I am working internally inside of the object - not referring to it externally - so I don't see how that could work.</p>
  <p>I intuitively feel like this should be possible, but I can't quite put my head around the syntax of it.</p>
  <p>An alternative would be to create this as a standalone function to which you <code>ref</code> an object, and it does the same type of thing, except in that scenario the object will have a name.</p>