Created
January 12, 2010 20:54
-
-
Save philiplaureano/275599 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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