Monthly Archives: October 2018

No InternalsVisibleTo, no problem – bypassing C# visibility rules with Roslyn

Both the C# compiler and the CLR/CoreCLR runtimes contain a bunch of rules that are in place to save us from ourselves (and to allow us to write code without needing to fully understand ECMA-334 C# Language Specification). That said, there are times where we want to do some things that are normally not allowed, and a good example of that is reaching into reflection to execute some private or internal code.

Today I wanted to show you how to do something quite cool – how to bypass the type/member visibility rules using the Roslyn compiler. In other words, how to get access to internal and private members without needing to use reflection or something like InternalsVisibleToAttribute.

Continue Reading

Convert null-valued results to 404 in ASP.NET Core MVC

ASP.NET Core MVC is pretty flexible in terms of how it expects you to return results from the API actions. You could return an IActionResult, which gives you certain control over the status code and the nature of the response (i.e. object or a file or a status code only and so on). You could return a concrete object instance, and the framework will serialize it to the relevant response format. Finally, you could also return the new ActionResult<T> which allows you to mix both of the previous approaches in a single method, giving you the best of both worlds.

Let’s have a look at what happens in the framework when you return a null object instance and how you can change that behavior.

Continue Reading