title: C# method to get Active Directory properties
body: <p>I want to create a method which accepts two arguments; First being the username, and the second being the name of the Active Directory property to return... The method exists in a separate class (<code>SharedMethods.cs</code>) and works fine if you define the property name locally in the method, however I can't workout how to pass this in from the second argument. </p>
  
  <p>Here is the method:</p>
  
  <pre><code>public static string GetADUserProperty(string sUser, string sProperty)
  {    
          PrincipalContext Domain = new PrincipalContext(ContextType.Domain);
          UserPrincipal User = UserPrincipal.FindByIdentity(Domain, sUser);
  
          var Property = Enum.Parse&lt;UserPrincipal&gt;(sProperty, true);
  
          return User != null ? Property : null;
  }
  </code></pre>
  
  <p>And the code that is calling it is as follows;</p>
  
  <pre><code>sDisplayName = SharedMethods.GetADUserProperty(sUsername, "DisplayName");
  </code></pre>
  
  <p>Currently <code>Enum.Parse</code> is throwing the following error: </p>
  
  <blockquote>
    <p>The non-generic method 'system.enum.parse(system.type, string, bool)' cannot be used with type arguments</p>
  </blockquote>
  
  <p>I can get it to work by removing the <code>Enum.Parse</code>, and manually specifying the property to retrieve as such:</p>
  
  <pre><code>public static string GetADUserProperty(string sUser, string sProperty)
  {
          PrincipalContext Domain = new PrincipalContext(ContextType.Domain);
          UserPrincipal User = UserPrincipal.FindByIdentity(Domain, sUser);
  
          return User != null ? User.DisplayName : null;
  }
  </code></pre>
  
  <p>Pretty sure I'm missing something obvious, thanks in advance for everyone's time.</p>