title: Converting to Enum using type variable
body: <p>Part of my software is using reflection. The issue I am having is that while I can get the type of the property, I cannot convert the string value using the <code>Type</code> from the PropertyInfo. This is the reason why i am using <code>t</code> in the sample code.</p>
  
  <p>The below code demonstrates the issue with the error message as a code comment. The syntax error is on the <code>t</code>. how can I fix this problem? thanks</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          Type t = typeof(Letters);
  
          Letters letter = "A".ToEnum&lt;t&gt;(); //-- Type or namespace expected.
      }
  }
  
  public enum Letters { A, B, C }
  
  //-- This is a copy of the EmunHelper functions from our tools library.
  public static class EnumExt
  {
      public static T ToEnum&lt;T&gt;(this string @string)
      {
          int tryInt;
          if (Int32.TryParse(@string, out tryInt)) return tryInt.ToEnum&lt;T&gt;();
          return (T)Enum.Parse(typeof(T), @string);
      }
      public static T ToEnum&lt;T&gt;(this int @int)
      {
          return (T)Enum.ToObject(typeof(T), @int);
      }
  }
  </code></pre>
  
  <p><strong>Solution:</strong></p>
  
  <p>The following works because when the value is set using reflection, the actual type of Enum is accepted. Where <code>myObject.Letter = result</code> is not.</p>
  
  <pre><code>Type t = currentProperty.PropertyType;
  Enum result = Enum.Parse(t, @string) as Enum;
  ReflectionHelper.SetProperty(entity, "LetterPropertyName", result);
  </code></pre>
  
  <p>Thank you all for your help.</p>