Skip to content

Instantly share code, notes, and snippets.

@philiplaureano
Created January 12, 2010 20:54
Show Gist options
  • Save philiplaureano/275599 to your computer and use it in GitHub Desktop.
Save philiplaureano/275599 to your computer and use it in GitHub Desktop.
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
{
class Program
{
static void Main(string[] args)
{
var filename = @"..\..\..\ConsoleClient\bin\Debug\SampleDomain.dll";
var assembly = AssemblyFactory.GetAssembly(filename);
// Intercept all object instantiations in the target assembly
assembly.InterceptNewInstances(ShouldInterceptNewOperator, ShouldInjectCurrentMethod);
AssemblyFactory.SaveAssembly(assembly, filename);
Console.WriteLine("{0} successfully modified.", filename);
return;
}
private static bool ShouldInterceptNewOperator(MethodReference constructor, TypeReference declaringType, MethodReference hostMethod)
{
// Only reference types and instance methods will be intercepted
var result = !declaringType.IsValueType &&
hostMethod.HasThis;
return result;
}
private static bool ShouldInjectCurrentMethod(MethodReference currentMethod)
{
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment