Examining the method body using Reflection in .NET C#

In this short post we saw how to extract the members of a class: constructors, properties, methods etc. Even more exciting is the fact that you can peek into the body of a method. Well, not the plain text C# or VB code, but the Intermediate Language – MSIL version of it.

The MethodBody object represents, as the name suggests, the body of a method including the local variables and the MSIL instructions. MethodBody is available on classes that derive from the MethodBase class, which are methods and constructors – MethodInfo and ConstructorInfo.

Consider the following Customer class:

Read more of this post

Using the Redis NoSql database with .NET Part 14: messaging in the .NET client

Introduction

In the previous post we saw how to execute transactions in the ServiceStack.Redis client. We could conclude that it was in fact extremely easy using the CreateTransaction, QueueCommand and Commit functions.

In this post we’ll look at how to use the Redis messaging function in the .NET client. You’ll see that it’s just as easy as executing transactions.

Read more of this post

Examining the Assembly using Reflection in .NET

The CLR code of a project is packaged into an assembly. We can inspect a range of metadata from an assembly that also the CLR uses to load and execute runnable code: classes, methods, interfaces, enumerations etc.

In order to inspect an Assembly in a .NET project we’ll first need to get a reference to the assembly in question. There are various ways to retrieve an assembly, including:

Assembly callingAssembly = Assembly.GetCallingAssembly();
Assembly entryAssembly = Assembly.GetEntryAssembly();
Assembly executingAssembly = Assembly.GetExecutingAssembly();

…where Assembly is located in the System.Reflection namespace.

Read more of this post

Using the Redis NoSql database with .NET Part 13: transactions in the .NET client

Introduction

In the previous post we looked at a simple monitoring tool we can use to check which commands are executed on the Redis server. Running MONITOR in a production system might not make sense since there’s no filtering and the commands will just fly by in the command window in quick succession. Also, the monitor slows down the server performance. The command monitor is most useful in an alpha server with not much traffic or for debugging a Redis-based application locally where the target database sits on the localhost. We then took this monitoring tool and checked what commands the .NET client generates while working with the various client interfaces. The most interesting case was to see how it handles objects with various keys and sets to keep track of the object IDs.

In this post we’ll look at how to execute transactions in the .NET client.

Read more of this post

Handling .NET exceptions in F#

When working with .NET languages from F# it can happen that the F# code needs to deal with .NET exceptions from the System namespace. The way to deal with those exceptions is about the same as in the case of F# exceptions but pattern matching in the with clause is slightly different:

Read more of this post

Custom exceptions in F#

We can build custom exceptions in F# with the exception keyword. We can also assign multiple parameters to it in a tuple:

exception ProductNotFoundException of string * int * int

Read more of this post

Using the Redis NoSql database with .NET Part 12: monitoring the database commands

Introduction

In the previous post we saw how to work with custom objects using the ServiceStack Redis client. The generic IRedisTypedClient works with the supplied type parameter and provides a large number functions to handle our objects in the Redis database. With this interface it almost feels like we’re working with a “traditional” database with its methods like Store, GetById, DeleteById, PopItemFromList etc. Redis may at first give the impression that it can only be used as a cache server with its keys and values. However, with proper usage of its data types Redis also serves as a “proper” database for modern applications.

In this post we’ll look at the question of how our objects are stored in Redis. After all when we went through the Redis commands before we didn’t see any command like GETBYID or STORE. Monitoring the calls sent to the Redis database will give us a hint.

Read more of this post

Explicit interface implementation in .NET

Introduction

The generic and well-known Dictionary object and its generic and thread-safe counterpart, i.e. the ConcurrentDictionary object both implement the generic IDictionary interface. The IDictionary interface has an Add method where you can insert a new key-value pair into a dictionary:

Dictionary<string, string> singleThreadedDictionary = new Dictionary<string, string>();
singleThreadedDictionary.Add("Key", "Value");

I can even rewrite the above code as follows:

Read more of this post

Using the Redis NoSql database with .NET Part 11: working with objects in the .NET client

Introduction

In the previous post we looked at the IRedisClient interface to communicate with Redis. We saw that it provides a more convenient way of dealing with Redis commands in code. It’s especially useful when working with lists, hashes, sets and sorted sets since those data types are exposed through properties. We can then process those variables almost like we do with their .NET equivalent objects, i.e. dictionaries, lists and hashsets. We also briefly looked at the two Redis client manager classes that provide access to a new Redis clients.

We still cannot seamlessly work with our custom objects. We’re still responsible for serialising and deserialising them through e.g. JSON. This is where the generic IRedisTypedClient enters the scene. We’ll look at that in this post.

Read more of this post

Monitor the file system with FileSystemWatcher in C# .NET

In this post we’ll look at how you can use the FileSystemWatcher object to monitor the Windows file system for various changes.

A FileSystemWatcher object enables you to be notified when some change occurs in the selected part of the file system. This can be any directory, such as “c:\” or any subdirectory under the C: drive. So if you’d like to make sure you’re notified if a change occurs on e.g. “c:\myfolder” – especially if it’s editable by your colleagues – then FileSystemWatcher is a good candidate.

Consider the following Console application:

Read more of this post

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

ARCHIVED: Bite-size insight on Cyber Security for the not too technical.