-
Notifications
You must be signed in to change notification settings - Fork 1
Home
ProxyProfiler
is a personal project that I started to study AOP. Developing the code, I decided to include Expression Trees for the purpose of study too. This quickly became a reasonable tool and I decided to make it a GitHub project.
ProxyProfiler
is a generic class that expects an interface as the generic type, because RealProxy
can work with MarshalByRefObject
. It is composed of two public static methods, Create
and GetHistory
.
To use ProxyProfiler
is simple:
Start calling the Create
method and you can get an instance of the same class used as argument, that implements the interface provided as generic parameter, but with ProxyProfiler
wrapping it.
Console.Interfaces.ITest testProfiler = ProxyProfiler.ProxyProfiler<Console.Interfaces.ITest>.Create(new Console.Classes.Test());
After that, just start calling your interface methods.
testProfiler.TestNoParams();
To start creating your custom profiler attributes, create a class that extends the base Console.Attributes.ProfilerAttribute
.
public class LogAttribute : ProxyProfiler.Attribute.ProfilerAttribute { }
ProfilerAttribute
expects the type of the object that will handle each event. This type must be the type of a class with an empty public constructor because it will inject an instance of that object on each event.
public LogAttribute(Type type) : base(type) { }
You can override three methods from ProfilerAttribute
.
public override void OnBeforeInvoke<T>(T profiler, MethodInfo methodToInvoke, object[] beforeInvokeArgs) { }
public override void OnAfterInvoke<T>(T profiler, MethodInfo invokedMethod, object[] beforeInvokeArgs, object[] afterInvokeArgs, object invokeResult) { }
public override void OnInvokeException<T>(T profiler, MethodInfo invokedMethod, Exception exception) { }
The type of T
will be the type supplied to ProfilerAttribute
constructor.
Create your class that will be injected in your attibute as you like. Just remind of the empty constructor.
public class Log : Console.Interfaces.ILog
{
public void Debug(string message) { }
public void Debug(string message, Exception ex) { }
}
After all that, go to your interface of your class that will be profiled and decorate the methods with the attribute.
public interface ITest
{
[Console.Attributes.Log(typeof(Console.Classes.Log))]
void TestNoParams();
}
You can use as many as attributes as you create on each method.