2

With postsharp is there a way from attribute to get variable value. This attribute will write some logs in database or nlog

[AOPTattribute($"The value of 'myint' is {myInt}")]
public void MyMethod()
{
    int myInt = (default) int;

    /*
    some code here  
    */
}

Thanks,

TheBoubou
  • 19,487
  • 54
  • 148
  • 236

1 Answers1

2

The short answer is that this is not possible using PostSharp Aspect Framework.

It would be possible with PostSharp SDK (direct MSIL manipulation), but the cost of development will probably be prohibitive. It you just want the variable values at the end of the method, it's simpler. If you want to intercept all direct assignments to variables, it's more difficult. If you want to also intercept indirect assignments through ref and out method calls, this is impossible.

Gael Fraiteur
  • 6,759
  • 2
  • 24
  • 31
  • It's better then nothing how get values at the end of the methods ? – TheBoubou Apr 03 '20 at 17:21
  • You can start with the project template https://github.com/postsharp/PostSharp.Community.HelloWorld/blob/master/PostSharp.Community.HelloWorld.Weaver/HelloWorldTask.cs. The Task will use the Weaver class. You can register an advice for AfterMethodBodySuccess. From the advice, you get local variables using MethodBodyDeclaration.GetLocalVariable, you box that to an object using InstructionWriter.EmitConvertToObject. Reach for support on Slack (https://postsharp-addins.slack.com/join/shared_invite/zt-d5il7j68-LRfbUOKuP7Hn2Amzb_nnDQ) if you plan to make the add-in open-source. – Gael Fraiteur Apr 04 '20 at 08:44