title: Set property of enum type given by string
body: <p>I got some enum's in my class, it's look like this one:</p>
  
  <pre><code>public enum A { A1, A2 }
  public enum B { B1, B2 }
  </code></pre>
  
  <p>Problem is when i try to read items from XML file (each field given by property name and property value - both string).</p>
  
  <p>Now i got my method which is setting value of single property (there is only part with isEnum check).</p>
  
  <pre><code>public A classfield
  {
    get;
    set;
  }
  public bool DynamicallySetItemProperty(string name, string value)
  {
    // value = "A1" or "A2"; - value given in string.
    // name = "classfield"; - name of property.
    PropertyInfo p = this.GetType().GetProperty(name);
    if (p.PropertyType.IsEnum)
    {
      var a = (A) Enum.Parse(typeof(A), value);
      p.SetValue(this, a);
    }
    return true;
  }
  </code></pre>
  
  <p>But in this one i'm not checking if field is A or B-enum.</p>
  
  <p>Is there any way to make my method checking type of enum and then parsing my string to this enum, something like this one:</p>
  
  <pre><code>public bool DynamicallySetItemProperty(string name,string value)
  {
    PropertyInfo p = this.GetType().GetProperty(name);
    if (p.PropertyType.IsEnum)
    {    
      var a = (p.GetType()) Enum.Parse(typeof(p.GetType()), value); // &lt;- it doesn't work
      p.SetValue(this, a);
    }
    return true;
  }
  </code></pre>
  
  <p>So i need to check property for its enum-type and then parse my string value to this enum-type without using  if/else or switch statement (it's problematic when i got a lot of enum types). Is there any easy way to do it?</p>