0

Anyone knows if it is possible to override or extend the Members.Add method on a System.DirectoryServices.AccountManagement.GroupPrincipal object?

I would like to do something like this

public new void Add(Principal principal)
{
    base.Add(principal);
    LogFile.WriteLine("Added {0} to group", principal.Name);   
}
akjoshi
  • 15,374
  • 13
  • 103
  • 121
cps001
  • 1
  • 1

2 Answers2

0

This is tricky, as you aren't trying to override a method on GroupPrincipal, but on a child object inside of GroupPrincipal, namely the PrincipalCollection object that represents the Members.

The only way to kind of do what you want is to put the Add method as just an ordinary method on your child object, something like:

public class EnhancedGroupPrincipal : Group Principal {    
    public void Add(Principal principal) {
        //your code to add and log
    }    
}

While you would have to know to use the alternate Add method, presumably you would know, so it would be OK.

Peter
  • 9,643
  • 6
  • 61
  • 108
0

The easiest way that I can figure out is using an extension method

public static void Add(this GroupPrincipal group, Principal child)
{
     group.Members.Add(child);
     LogFile.WriteLine("Added {0} to group", child.Name);
     //Do whatever else you have to do...
}
jmcm
  • 23
  • 5