Actually I have few question to be easier to understand what I'm asking about I have to show my code first.
public static void Main(string[] args)
{
CustomClass customClass = new CustomClass();
customClass.SomeMethod("asdas", true, 30.5);
}
public class CustomClass
{
[MyAttribute]
public Boolean SomeMethod(String a, Boolean b, Double c)
{
return true;
}
}
public class MyAttribute : Attribute
{
public MyAttribute()
{
SomeIntercepter.InterceptEverything();
}
public void DoSomethingBeforeMethodexecutes()
{
....
}
public void DoSomethingAfterMethodExecutes()
{
....
}
}
public class SomeIntercepter
{
public static void InterceptEverything()
{
StackTrace stackTrace = new StackTrace();
var method = stackTrace.GetFrame(2).GetMethod();
var parameters = method.GetParameters();
if (parameters.Length > 3)
return;
String cacheKey = method.Name;
for (int i = 0; i < parameters.Length; i++)
{
//HOW TO GET THE PARAMETER DATA ASSIGNED
cacheKey += "_" + parameters[i];
}
}
}
So what I try to do. I try to intercept method on each it call and do something based on the incomming data of the method which is marked with [MyAttribute]
. So I access the method through the StackTrace and try to get all incomming data with GetParameters.
And here is my questions:
1) How to object[] of all incomming data of the SomeMethod
in the InterceptEverything()
2) How to say to the MyAttribute
to run before marked method with MyAttribute
to run method DoSomethingBeforeMethodexecutes()
3) How to say to the MyAttribute
to run after marked method with MyAttribute
to run method DoSomethingAfterMethodexecutes()
Thanks for any advice.