2

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.

Maris
  • 4,608
  • 6
  • 39
  • 68

1 Answers1

3

You cannot get the values from any particular frame via ParameterInfo. This facility does not exist.

Also: attributes do not execute. Unless you are using something like postsharp, they are just information. To get them to execute you must explicitly write reflection code to do that.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • But how can I get it from StackTrace? – Maris Mar 15 '13 at 10:51
  • 1
    @Maris there's a question you jumped: *can* I get it from StackTrace. To which the answer is: no, you cannot. – Marc Gravell Mar 15 '13 at 10:52
  • @Maris: You cannot. You need a debugger or profiler to get that info. – leppie Mar 15 '13 at 10:52
  • @MarcGravell: Or derive from a security attribute and pass `Demand` to the constructor (comment intentionally vague) – leppie Mar 15 '13 at 10:53
  • @leppie yeah, I was deliberately avoiding mentioning security attributes, because a: that's evil, b: they don't run in full trust, c: that's evil – Marc Gravell Mar 15 '13 at 10:54
  • Ok maybe not stack trace. Can I get somehow in InterceptEverything() the entire data of method which calls it? – Maris Mar 15 '13 at 10:54
  • @Maris no, you cannot, unless you are a debugger – Marc Gravell Mar 15 '13 at 10:54
  • @MarcGravell: Pretty sure they run in full trust. Got bitten by it recently. – leppie Mar 15 '13 at 10:56
  • I have a lot of classes, each class have few methods I want to say all methods of those classes to run in something like container. which will do something before the method runs(what it will do is based on the incomming data of the method), and do something after it return the value and do something(based on what data was returned). Any ideas how can I do it? – Maris Mar 15 '13 at 11:04
  • @leppie my memory of that is different, but frankly it is a faff to set up a test project and I'm lazy – Marc Gravell Mar 15 '13 at 11:06
  • @Maris you might want to look at what things like postsharp bring to that, or maybe any of the decorator-based frameworks. There is nothing inbuilt that will do that for you. – Marc Gravell Mar 15 '13 at 11:06
  • Can you show some code-example of postsharp that will help me to do it? – Maris Mar 15 '13 at 11:10
  • http://stackoverflow.com/questions/737833/how-do-i-add-arguments-to-postsharp-attributes – Marc Gravell Mar 15 '13 at 11:14
  • Hmmm. Looks like it will help, Thanks a lot! – Maris Mar 15 '13 at 11:15