Using a class versus struct as a dictionary key

Dictionary<TKey, TValue> uses an IEqualityComparer<TKey> for comparing the keys. If you do not explicitly specify the comparer when you construct the dictionary, it will use EqualityComparer<TKey>.Default. Since neither MyClass nor MyStruct implement IEquatable<T>, the default equality comparer will call Object.Equals and Object.GetHashCode for comparing instances. MyClass is derived from Object, so the implementation will use …

Read more

How can i configure JSON format indents in ASP.NET Core Web API

.NET Core 2.2 and lower: In your Startup.cs file, call the AddJsonOptions extension: services.AddMvc() .AddJsonOptions(options => { options.SerializerSettings.Formatting = Formatting.Indented; }); Note that this solution requires Newtonsoft.Json. .NET Core 3.0 and higher: In your Startup.cs file, call the AddJsonOptions extension: services.AddMvc() .AddJsonOptions(options => { options.JsonSerializerOptions.WriteIndented = true; }); As for switching the option based on …

Read more

Store does not implement IUserRoleStore ASP.NET Core Identity

In Startup.cs, I was missing AddRoles so services.AddDefaultIdentity<PortalUser>() .AddEntityFrameworkStores<ApplicationDbContext>(); should be services.AddDefaultIdentity<PortalUser>() .AddRoles<IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>(); Note: The order is critical. AddRoles must come before AddEntityFrameworkStores

How can I determine property types using reflection?

What type are you interested in? The return type of the method/property/event etc? If so, I don’t think there’s anything in MemberInfo to let you get at it directly – you’ll need to cast and use MethodInfo.ReturnType, PropertyInfo.PropertyType, FieldInfo.FieldType, EventInfo.EventHandlerType and any others I’ve forgotten. (Remember that types themselves can be members. Not sure what …

Read more