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 (SharedMethods.cs
) 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.
Here is the method:
public static string GetADUserProperty(string sUser, string sProperty)
{
PrincipalContext Domain = new PrincipalContext(ContextType.Domain);
UserPrincipal User = UserPrincipal.FindByIdentity(Domain, sUser);
var Property = Enum.Parse<UserPrincipal>(sProperty, true);
return User != null ? Property : null;
}
And the code that is calling it is as follows;
sDisplayName = SharedMethods.GetADUserProperty(sUsername, "DisplayName");
Currently Enum.Parse
is throwing the following error:
The non-generic method 'system.enum.parse(system.type, string, bool)' cannot be used with type arguments
I can get it to work by removing the Enum.Parse
, and manually specifying the property to retrieve as such:
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;
}
Pretty sure I'm missing something obvious, thanks in advance for everyone's time.