Skip to content

Compile time decorator pattern via IL rewriting

Notifications You must be signed in to change notification settings

tsliang/MethodDecorator

 
 

Repository files navigation

This is an add-in for Fody

Icon

Compile time decorator pattern via IL rewriting

Introduction to Fody

Nuget

Nuget package http://nuget.org/packages/MethodDecorator.Fody

To Install from the Nuget Package Manager Console

PM> Install-Package MethodDecorator.Fody

Your Code

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

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor)]
public class InterceptorAttribute : Attribute, IMethodDecorator
{
    public void OnEntry(MethodBase method)
    {
        TestMessages.Record(string.Format("OnEntry: {0}", method.DeclaringType.FullName + "." + method.Name));
    }

    public void OnExit(MethodBase method)
    {
        TestMessages.Record(string.Format("OnExit: {0}", method.DeclaringType.FullName + "." + method.Name));
    }

    public void OnException(MethodBase method, Exception exception)
    {
        TestMessages.Record(string.Format("OnException: {0} - {1}: {2}", method.DeclaringType.FullName + "." + method.Name, exception.GetType(), exception.Message));
    }
}

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

What gets compiled

public class Sample
{
	public void Method()
	{
	    MethodBase method = methodof(Sample.Method, Sample);
	    InterceptorAttribute attribute = (InterceptorAttribute) method.GetCustomAttributes(typeof(InterceptorAttribute), false)[0];
	    attribute.OnEntry(method);
	    try
	    {
	        Debug.WriteLine("Your Code");
	        attribute.OnExit(method);
	    }
	    catch (Exception exception)
	    {
	        attribute.OnException(method, exception);
	        throw;
	    }
	}
}

Icon

Icon courtesy of The Noun Project

About

Compile time decorator pattern via IL rewriting

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published