LinFu Archive

Since a few days a I’m having a close look to LinFu 2.2 and talking with Philip Laureano, the creator of this amazing framework. LinFu 2.2 is very different than LinFu 1. Now is talking directly with Mono.Cecil to modify types in runtime, weave types for friends :-)

With LinFu 2.2 you can do:

  • Property interception
  • New operator interception
  • Third Party methods interception

This interception is made without proxies, the assembly is changed in runtime, and then, you’re able to add hooks whatever you want.

LinFuEngine linfu = new LinFuEngine("MethodInterception.exe");

var targetType = linfu.GetType<Foo>();
var typeName = targetType.Name;

targetType.InterceptMethodCalls(t => t.Name.Contains(typeName),
                m => m.DeclaringType.Name.Contains(typeName) && m.Name == "DoSomething",
        methodCall => methodCall.DeclaringType.Name == "Console" && methodCall.Name == "WriteLine");

var instance = linfu.CreateInstanceModified<Foo>();

var host = (IMethodReplacementHost) instance;

var interceptor = new Interceptor();

host.MethodReplacementProvider = new MethodInterceptorProvider(interceptor);
MethodInfo targetMethod = linfu.CreateModifiedType<Foo>().GetMethod("DoSomething");
targetMethod.Invoke(instance, null);

Console.WriteLine("Was intercepted :{0}", interceptor.HasBeenCalled);

I was trying to wrap a little to LinFu because right now is low level API, so to get the complete code you can check out here.

In this example we are modifying the assembly and Interception the Console.WriteLine method. The example still being low level but you’ve to be patience, LinFu is not released yet, and you can begin using it and proposing features to make it easier.

NHibernate is using LinFu 1 for dynamic proxy creation. I’m working (Philip also is helping a lot !) to do LinFu2.2 make works with NHibernate an pass the whole suite of NHibernate.

Hope you find this interesting. For more info, catch in the Philip blog !

Be the first to comment