I'm trying to implement my own aop-framework (like PostSharp). And stuck with one problem.
Say there is an attribute:
[AttributeUsage(AttributeTargets.Method)]
public class LogAttribute : Attribute
{
public LogAttribute(int level, Type type)
{
// init fields...
}
public virtual void OnEnter(LogEventArg e)
{
// do some stuff...
}
}
which is applied to one of the method:
[Log(3, typeof(Trace))]
static void TestMethod1(string s)
{
// do some stuff...
}
And now the question: how to convert this to:
static void TestMethod1(string s)
{
var t = new LogAttribute(3, typeof(Trace));
t.OnEnter()
// do some stuff...
}
I would like to strip attribute from method and not to use something like this:
var m = System.Reflection.MethodBase.GetCurrentMethod();
var attr = (LogAttribute) Attribute.GetCustomAttribute(m, typeof(Log));
With Mono.Cecil I have CustomAttributes.Constructor (MethodReference) and CustomAttributes.ConstructorArguments, which have 'Type' and 'Value' properties. But how to convert it to il instructions???
The only thing that I can write is:
il.InsertBefore(inst0, Instruction.Create(OpCodes.Newobj, attr.Constructor));
But how to pass arguments to this constructor, exactly as it was declared? I need a versatile option that is not tied to a specific constructor implementation.