What is Thread.CurrentPrincipal, and what does it do?

Thread.CurrentPrincipal is the way .NET applications represent the identity of the user or service account running the process. It can hold one or more identities and allows the application to check if the principal is in a role through the IsInRole method. Most authentication libraries in .NET will verify the user’s credentials and set this … Read more

what is the correct way to cancel multiple tasks in c#

Yeah, what you said about using a single CancellationToken is correct. You can create a single CancellationTokenSource and use its CancellationToken for all of the tasks. Your tasks should check the token regularly for cancellation. For example: const int NUM_TASKS = 4; CancellationTokenSource cts = new CancellationTokenSource(); CancellationToken ct = cts.Token; Task[] tasks = new … Read more

Is the new feature of C# 4.0 – “Optional Parameters” CLS-Compliant?

Optional arguments are “sort-of” CLS-compliant. Methods with optional arguments are legal and can be successfully compiled with the CLSCompliant attribute, but callers of those methods don’t necessarily need to take account of the default parameter values or the optional attribute. (In which case those methods would behave in exactly the same way as standard methods, … Read more

How to get fields and their values from a static class in referenced assembly

Using reflection, you will need to look for fields; these are not properties. As you can see from the following code, it looks for public static members: class Program { static void Main(string[] args) { Type t = typeof(A7); FieldInfo[] fields = t.GetFields(BindingFlags.Static | BindingFlags.Public); foreach (FieldInfo fi in fields) { Console.WriteLine(fi.Name); Console.WriteLine(fi.GetValue(null).ToString()); } Console.Read(); … Read more

Mixins with C# 4.0

You can create mixin-like constructs in C# 4.0 without using dynamic, with extension methods on interfaces and the ConditionalWeakTable class to store state. Take a look here for the idea. Here’s an example: public interface MNamed { // required members go here } public static class MNamedCode { // provided methods go here, as extension … Read more