SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Matt Ward

Using Fody with SharpDevelop

Fody is a NuGet addin, written by Simon Cropp, that will weave IL instructions into your assembly after it has been compiled. IL weaving is the creation and manipulation of IL without the use of a compiler. To do the IL weaving Fody uses a MSBuild task and Mono.Cecil.

Fody is a general purpose IL weaver and is extended using Fody addins, which are also NuGet packages. One example is the PropertyChanged addin which will inject INotifyPropertyChanged code for your class properties into your assembly without you having to write the code. Another example is the NullGuard addin which will add null checks to your code.

For the full list of Fody addins see the list on the Fody GitHub page:

Now let us take a look at using the NullGuard addin with SharpDevelop.

You will need to use the latest version of SharpDevelop 4.4. Any version from 4.4.0.9711 or above should work. Previous versions of SharpDevelop would not correctly configure the path to the Fody assembly which would cause the project to fail to compile.

Installing Fody

Fody is a NuGet package so use either the Manage Packages dialog or the Package Management console to install the Fody nuget package into your project.

Fody NuGet Package in Manage Packages dialog

Installing the Fody NuGet package will add a FodyWeavers.xml and a Fody.targets file to your project. The FodyWeavers.xml file configures the Fody addins that will be used. The project itself will be updated so it imports the Fody.targets file which is a custom MSBuild targets file. Also a FodyPath property will be added to your project so the Fody.dll assembly can be located and used by the Fody MSBuild targets file.

Installing NullGuard addin

Search for the NullGuard.Fody in the Manage Packages dialog or use the Package Management Console and install it.

Fody NuGet Package in Manage Packages dialog

Installing the NullGuard.Fody NuGet package will update the FodyWeavers.xml file so the NullGuard addin will be used by Fody.

<?xml version="1.0" encoding="utf-8"?>
<Weavers>
  <NullGuard />
</Weavers>

NullGuard Generated Code

Now let us take a look at what code is generated by Fody and the NullGuard addin when the following class is included in the project:

using System;

namespace NullGuardTest
{
    public class MyClass
    {
        public void MyMethod(string parameter)
        {
        }
    }
}

Compiling the project will now weave in null checks into your assembly. If you open your assembly into ILSpy you can view the generated code, which is shown below:

using System;
using System.Diagnostics;
namespace NullGuardTest
{
    public class MyClass
    {
        public void MyMethod(string parameter)
        {
            Debug.Assert(parameter != null, "[NullGuard] parameter is null.");
            if (parameter == null)
            {
                throw new ArgumentNullException("parameter", "[NullGuard] parameter is null.");
            }
        }
    }
}

The NullGuard addin has added null checks for the parameter being passed into the method without you having to write the code yourself.

The generated assembly IL can be further controlled by adding attributes to your class. Further information on the supported attributes that are available can be found on the NullGuard GitHub page.

That is the end of the quick introduction to using Fody with SharpDevelop.

Published Dec 01 2013, 03:53 PM by MattWard
Filed under:

Comments

No Comments
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.