primaryGroupToken via C# from Active Directory (AD)

I needed to change Primary Group of the Active Directory user programmatically (via C#).

I found a good example in PowerShell.

But it wasn’t obvious for me on how to convert the following code fragment into C#:

$NewGroup = [ADSI]"LDAP://CN=Domain Guests,CN=Users,$DomainNC"
$NewGroup.GetInfoEx(@("primaryGroupToken"), 0)
$NewGroupToken = $NewGroup.Get("primaryGroupToken")

So here is it, just in case it isn’t obvious for you as well ;)

group.Invoke("GetInfoEx", new object[] { new object[] { "primaryGroupToken" }, 0 });
object primaryGroupToken = group.Invoke("Get", new object[] { "primaryGroupToken" });