Skip to content

Instantly share code, notes, and snippets.

protected override void ModifyTargetMethod(MethodDefinition targetMethod)
{
// In this example, we are going to redirect all Console.WriteLine calls
// to the FakeConsole.WriteLine() method
var module = targetMethod.Module;
var body = targetMethod.Body;
// In order to use FakeConsole.WriteLine(), we need to use Cecil to "import"
// the method into the modified assembly's module
var writeLine = module.Import(typeof (FakeConsole).GetMethod("WriteLine"));
public interface IExceptionHandlerInfo
{
Exception Exception { get; }
IInvocationInfo InvocationInfo { get; }
object ReturnValue { get; set; }
bool ShouldSkipRethrow { get; set; }
}
public class SampleExceptionHandler : IExceptionHandler
{
public bool CanCatch(IExceptionHandlerInfo exceptionHandlerInfo)
{
return true;
}
public void Catch(IExceptionHandlerInfo exceptionHandlerInfo)
{
var exception = exceptionHandlerInfo.Exception;
@philiplaureano
philiplaureano / gist:954549
Created May 4, 2011 00:45
A sample CSProj file modification for dynamically intercepting thrown exceptions with LinFu.AOP
<PropertyGroup>
<PostWeaveTaskLocation>$(MSBuildProjectDirectory)\$(OutputPath)\..\..\..\lib\LinFu.Core.dll</PostWeaveTaskLocation>
</PropertyGroup>
<UsingTask TaskName="PostWeaveTask" AssemblyFile="$(PostWeaveTaskLocation)" />
<Target Name="AfterBuild">
<PostWeaveTask TargetFile="$(MSBuildProjectDirectory)\$(OutputPath)$(MSBuildProjectName).dll" InterceptAllExceptions="true" />
</Target>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.Cecil;
using LinFu.AOP.Interfaces;
using LinFu.AOP.Cecil;
namespace CSLInjectionDemo
{