forked from reflection-emit/Cauldron
-
Notifications
You must be signed in to change notification settings - Fork 18
Cecilator
Alexei Stryker edited this page Jun 27, 2018
·
3 revisions
Cecilator is a wrapper for Mono.Cecil that aims to make IL-weaving less painful and more productive with less code.
For example if you want to weave a try-catch-finally handler in an existing method, you can code like this:
method
.NewCoder()
.Context(x =>
{
foreach (var item in attributeMethods)
x.SetValue(item.Variable, y => y.NewObj(item.Method));
})
.Try(x =>
{
x.OriginalBody();
})
.Catch(BuilderTypes.Exception.BuilderType, (eCoder, e) =>
{
eCoder.Rethrow();
})
.Finally(x =>
{
foreach (var item in attributeMethods)
x.Load(item.Variable).Call(item.OnExitMethod);
})
.EndTry()
.Return()
.Replace();
The produced IL-code looks like this
// Method begins at RVA 0x3aac
// Code size 26 (0x1a)
.maxstack 1
.locals init (
[0] class Cauldron.Interception.Test.Interceptors.TestMethodInterceptorAttribute var_897072c1x8472x4031xa359xc8a4699da349,
[1] int32 '<>returnValue'
)
IL_0000: newobj instance void Cauldron.Interception.Test.Interceptors.TestMethodInterceptorAttribute::.ctor()
IL_0005: stloc.0
.try
{
.try
{
IL_0006: ldc.i4 3434
IL_000b: stloc.1
IL_000c: leave.s IL_0018
} // end .try
catch [mscorlib]System.Exception
{
IL_000e: rethrow
} // end handler
} // end .try
finally
{
IL_0010: ldloc.0
IL_0011: call instance void Cauldron.Interception.Test.Interceptors.TestMethodInterceptorAttribute::OnExit()
IL_0016: endfinally
} // end handler
IL_0017: nop
IL_0018: ldloc.1
IL_0019: ret