Browsing posts in: c#

Using async disposable and async enumerable in frameworks older than .NET Core 3.0

One of the awesome features introduced in .NET Core 3.0 and C# 8.0 are async streams. The feature consists of two parts – async disposable, for async clean up, as well as async enumerable, for async iteration.

Normally, the C# language features are backwards compatible and can be used regardless of the runtime framework being targeted. In this particular case, however, the newly introduced types that are needed for async streams feature to work, such as for example IAsyncDisposable or IAsyncEnumerator<T>, were only added in .NET Core 3.0, restricting the usage of the features to that runtime, and later.

Let's have a look at how you can still benefit from async disposable and async enumerable on older frameworks.

Continue Reading

“Implementing” a non-public interface in .NET Core with DispatchProxy

Reflection is a tremendously powerful concept in .NET, which every C# developer, sooner or later, ends up working with. It is very useful for a lot of perfectly honest scenarios such as for example assembly scanning, type discovery or all kinds of application composability features.

However, it can also be used to circumvent the public API surface of the dependencies you consume – to modify them or gain access to things that the author of your dependency didn’t exactly envision. That said, this process of “hacking around” is very typical for C# development world and, albeit somewhat risky, it sometimes might be the only way out of a coding jam you are in.

Things start to get interesting if you are forced to implement a non-public (for example internal) interface. At that point, the “basic” reflection can’t help anymore, so let’s have a look at how we can do it.

Continue Reading

EditorConfig support in OmniSharp and C# extension VS Code

Recently, I blogged about Roslyn analyzers support in OmniSharp and C# extension VS Code and some background on how that feature came together.

Today I am pleased to announce that in the latest OmniSharp release – 1.33.0, where we have shipped another large feature (in fact, the 2nd most requested feature in OmniSharp repo, after, well, analyzers) and that is support for EditorConfig.

Between then, and the time it took me to finally write this post, we actually have 1.34.0 which is even better – and is in fact included in the corresponding release 1.21.0 of the C# extension for VS Code.

More after the jump.

Continue Reading

Building a C# Interactive shell in a browser with Blazor (WebAssembly) and Roslyn

In this post I wanted to show you how to write and embed a C# interactive shell (a REPL – read-evaluate-print-loop) in a browser, on top of WebAssembly.

The REPL will give you fully fledged C# interactive development playground, while still being completely sandboxed in the browser environment. I originally wrote this example for my session at Dotnet Cologne on May 10 this year.

More after the jump.

Continue Reading

Collectible assemblies in .NET Core 3.0

Since the beginning of .NET Core, the one feature that I have been most anxiously waiting for, has been support for collectible assemblies. It took a while (a while!), but finally, in .NET Core 3.0 (at the time of writing 3.0.0-preview-27122-01 from 2018-12-04), it’s here.

It’s going to be a killer functionality, that will support some excellent use cases in .NET Core – especially around application plugins, extensibility and dynamic assembly generation.

Let’s have a quick look at how we can load and unload assemblies in .NET Core.

Continue Reading

Using Roslyn C# Completion Service programmatically

I am involved in a few open source projects built around the Roslyn compiler. One of those projects is OmniSharp, which brings intellisense and C# language services to a number of editors out there, allowing them to provide for their users a rich C# code authoring experience.

Which actually brings me to the point of today’s post. Roslyn is a compiler-as-a-service that you can embed in your own app, and when you do that, you could reach into its C# language services (more specifically, CompletionService) and easily build your own C# intellisense engine.

However, this is not really documented, so I wanted to use this post to show you how you can get started with that.

Continue Reading

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

dotnet-script – now on .NET Core 2.1 runtime, with C# 7.3 support and more!

In the previous post, I announced that dotnet-script is now built as global tool and can be installed using .NET Core 2.1 preview SDK. However, by that time it was still based on .NET Core 2.0 runtime (it was possible to build and install .NET Core 2.0 tools using .NET Core 2.1 preview SDK).

Now we have even more cool news to announce – because dotnet-script is now a true .NET Core 2.1 application, and can be used to run scripts on top of .NET Core 2.1 runtime. We release quite often so let me do a quick rundown of all the new features since my last blog post.

Continue Reading

Easy way to create a C# lambda expression from a string (with Roslyn)

I’ve worked on quite a lot of projects over the years, with many different teams, and one of the questions that keeps coming back to me over and over with a high degree of regularity is how to load a C# lambda from a string – for example from a configuration file.

This is not surprising, because being able to do that can give you a tremendous amount of flexibility in your code, as it would (for the lack of better word) unlock the possibility to alter business logic from the configuration level, without having to recompile and redeploy your application.

Historically, this has been possible but also quite a painful task. Today I wanted to show you a remarkably simple solution to this problem – with the help of the Roslyn compiler Nuget packages.

Continue Reading