title: How to cast from GroupPrincipal base to GroupPrincipalsEx
body: <p>I have GroupPrincipal extension that adds several extensionAttributes:</p>
  
  <pre><code>[DirectoryObjectClass("group")]
  [DirectoryRdnPrefix("CN")]
  
  public class GroupPrincipalsEx : GroupPrincipal
  {
      public GroupPrincipalsEx(PrincipalContext context) : base(context) { }
  
      public GroupPrincipalsEx(PrincipalContext context, string samAccountName)
          : base(context, samAccountName)
      {
      }
  
      [DirectoryProperty("ExtensionAttribute1")]
      public string ExtensionAttribute1
      {
          get
          {
              if (ExtensionGet("ExtensionAttribute1").Length != 1)
                  return null;
  
              return (string)ExtensionGet("ExtensionAttribute1")[0];
  
          }
          set { this.ExtensionSet("ExtensionAttribute1", value); }
      }
  
      [DirectoryProperty("ExtensionAttribute2")]
      public string ExtensionAttribute2
      {
          get
          {
              if (ExtensionGet("ExtensionAttribute2").Length != 1)
                  return null;
  
              return (string)ExtensionGet("ExtensionAttribute2")[0];
  
          }
          set { this.ExtensionSet("ExtensionAttribute2", value); }
      }
  
      [DirectoryProperty("ExtensionAttribute3")]
      public string ExtensionAttribute3
      {
          get
          {
              if (ExtensionGet("ExtensionAttribute3").Length != 1)
                  return null;
  
              return (string)ExtensionGet("ExtensionAttribute3")[0];
  
          }
          set { this.ExtensionSet("ExtensionAttribute3", value); }
      }
  
      [DirectoryProperty("ExtensionAttribute4")]
      public string ExtensionAttribute14
      {
          get
          {
              if (ExtensionGet("ExtensionAttribute4").Length != 1)
                  return null;
  
              return (string)ExtensionGet("ExtensionAttribute4")[0];
  
          }
          set { this.ExtensionSet("ExtensionAttribute4", value); }
      }
  }
  </code></pre>
  
  <p>I need to get list of groups that specific user is member of and extensionAttribute1 of that group is specific value. It should return array of custom objects (group SamAccountName, DN, extensionAttributes 1-4).</p>
  
  <p>I know how to get all groups user is member, but I'm having problem to cast GroupPrincipal to GroupPrincipalsEx.</p>
  
  <pre><code>public List&lt;GroupPrincipalsEx&gt; GetGroups(string userName, Boolean Recurent = false)
      {
          List&lt;GroupPrincipalsEx&gt; result = new List&lt;GroupPrincipalsEx&gt;();
  
          // establish domain context
          PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain);
  
          // find your user
          UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, userName);
  
          // if found - grab its groups
          if (user != null)
          {
              PrincipalSearchResult&lt;Principal&gt; groups = Recurent ? user.GetAuthorizationGroups() : user.GetGroups();
  
              // iterate over all groups
              foreach (Principal p in groups)
              {
                  // make sure to add only group principals
                  if (p is GroupPrincipal)
                  {
                      //I need a way to cast p from GroupPrincipal to GroupPrincipalsEx and then add it to array
                      GroupPrincipalsEx gp = (GroupPrincipalsEx)p;
                      if(gp.ExtensionAttribute1 == "Specific Value")
                         result.Add(gp);
                  }
              }
          }
  
          return result;
      }
  </code></pre>
  
  <p>Any idea how to do this without using DirectoryEntry?</p>