Search code examples
fody

Fody MethodDecorator not working


I am trying to create a method decorator using Fody but it gives me the following error:

enter image description here

I have taken specific care to not wrap my IMethodDecorator inside any namespace as has been mentioned in a lot of places online. Following is the sample code I am trying in a console app.

IMethodDecorator

using System;
using System.Reflection;


    public interface IMethodDecorator
    {
        void OnEntry(MethodBase method);
        void OnExit(MethodBase method);
        void OnException(MethodBase method, Exception exception);
    }

MethodDecoratorAttribute

using System;
using System.Diagnostics;
using System.Reflection;
using FODYPOC;

// Atribute should be "registered" by adding as module or assembly custom attribute
[module: MethodDecorator]

namespace FODYPOC
{
// Any attribute which provides OnEntry/OnExit/OnException with proper args
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Assembly | AttributeTargets.Module)]
    public class MethodDecoratorAttribute : Attribute, IMethodDecorator
    {
        // instance, method and args can be captured here and stored in attribute instance fields
        // for future usage in OnEntry/OnExit/OnException

        public MethodDecoratorAttribute() { }

        public void OnEntry(MethodBase method)
        {
            Console.WriteLine();
        }

        public void OnExit(MethodBase method)
        {
            Console.WriteLine();
        }

        public void OnException(MethodBase method, Exception exception)
        {
            Console.WriteLine();
        }
    }

    public class Sample
    {
        [MethodDecorator]
        public void Method()
        {
            Debug.WriteLine("Your Code");
        }
    }
}

Can someone point me in the right direction. It looks pretty simple to implement and I know I am making a very silly mistake somewhere.


Solution

  • Apparently the latest version of MethodDecorator.Fody (Version 0.9.0.6 currently) was not working. Downgrading the version to version 0.8.1.1 fixed the issue for me.

    After a little more investigation, it appears that the interface method signatures were different in the two versions. So when I had the new package, it was not expecting MethodBase as a parameter and due to not finding anything that matches the interface it expects, it was throwing the error.