Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BinToss/923e679df387b065a65dc8fdee959d7d to your computer and use it in GitHub Desktop.
Save BinToss/923e679df387b065a65dc8fdee959d7d to your computer and use it in GitHub Desktop.
C# - Can I use IEnumerable<T>.Aggregate<T>(func()) to combine flag enum values?
#!meta
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"aliases":[],"name":"csharp"}]}}
#!csharp
/** Q: Can I use IEnumerable<T>.Aggregate<T>(func()) to combine flag enum values?
A:
*/
using System.Collections.Concurrent;
PROCESS_ACCESS_RIGHTS[] AccessRightsRequested = { PROCESS_ACCESS_RIGHTS.PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_ACCESS_RIGHTS.PROCESS_VM_READ, PROCESS_ACCESS_RIGHTS.PROCESS_DUP_HANDLE };
ConcurrentBag<PROCESS_ACCESS_RIGHTS> AccessRightsGranted = new();
var parallelLoopResult = Parallel.ForEach(AccessRightsRequested, accessRight =>
{
try
{
AccessRightsGranted.Add(accessRight);
}
catch (Exception ex)
{
Console.Error.WriteLine($"Failed to open a temporary process handle to check permissible access rights. {ex}");
}
});
parallelLoopResult.Display();
/**md System.Threading.Tasks.ParallelLoopResult
| | |
| --------------------- | ------- |
| IsCompleted | True |
| LowestBreakIteration | <null> |
*/
AccessRightsGranted.Display();
/** | index | value
| ----- | ----
| 0 | PROCESS_VM_READ
| 1 | PROCESS_QUERY_LIMITED_INFORMATION
| 2 | PROCESS_DUP_HANDLE
*/
PROCESS_ACCESS_RIGHTS usableRights = AccessRightsGranted.Aggregate((a, b) => a | b);
usableRights.Display();
/** PROCESS_VM_READ, PROCESS_DUP_HANDLE, PROCESS_QUERY_LIMITED_INFORMATION
*/
[Flags]
[global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Windows.CsWin32", "0.2.229-beta+f8b5498ab6")]
public enum PROCESS_ACCESS_RIGHTS : uint
{
PROCESS_TERMINATE = 0x00000001,
PROCESS_CREATE_THREAD = 0x00000002,
PROCESS_SET_SESSIONID = 0x00000004,
PROCESS_VM_OPERATION = 0x00000008,
PROCESS_VM_READ = 0x00000010,
PROCESS_VM_WRITE = 0x00000020,
PROCESS_DUP_HANDLE = 0x00000040,
PROCESS_CREATE_PROCESS = 0x00000080,
PROCESS_SET_QUOTA = 0x00000100,
PROCESS_SET_INFORMATION = 0x00000200,
PROCESS_QUERY_INFORMATION = 0x00000400,
PROCESS_SUSPEND_RESUME = 0x00000800,
PROCESS_QUERY_LIMITED_INFORMATION = 0x00001000,
PROCESS_SET_LIMITED_INFORMATION = 0x00002000,
PROCESS_ALL_ACCESS = 0x001FFFFF,
PROCESS_DELETE = 0x00010000,
PROCESS_READ_CONTROL = 0x00020000,
PROCESS_WRITE_DAC = 0x00040000,
PROCESS_WRITE_OWNER = 0x00080000,
PROCESS_SYNCHRONIZE = 0x00100000,
PROCESS_STANDARD_RIGHTS_REQUIRED = 0x000F0000,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment