c# - Have PostSharp help to log IF statements -


the behavior i'm looking following:

[ifelselogging] public void foo(string bar) {     if (bar == "somedumbstringbecausethisishorriblewayofdoingthings")     {         // here aspect log if statement above true,         // , took path     }     else     {         // here aspect log if statement above false,         // , took path     } } 

if there way hijack if functionality, able adorn logging postsharp uses , dust off hands. other reason i'm adding question i'm not sure i'm being clear i'm asking.

this project done jr developer. task not refactor document code level of flow charts. given nature of code, lack of descriptions, poor techniques , practices, nature of project, etc... here things i'm struggling with:

  • i can't debug , step through code locally
  • i can't create unit tests , refactor safely
  • i can't understand code does!
  • i have no original requirement documents understand goal of functionality is

so i'm looking use postsharp give me code paths , unit test criteria don't know how else it.

on thought, possible have aspect triggers event when if statement found?

aspect oriented programming postsharp allow creator interceptors hook code @ well-defined points method calls , property access. done rewriting il generated source code.

in case looking specific if statements not recognized in generated il. here generated sample method:

           nop           ldarg.1                ldstr       "somedumbstringbecausethisishorriblewayofdoingthings"           call        system.string.op_equality           stloc.0           ldloc.0           brfalse.s   notequal           nop  **true branch**            br.s        return notequal: nop  **false branch**  return:   ret 

however, non-optimized version. optimizing compiler generates code:

           ldarg.1           ldstr       "somedumbstringbecausethisishorriblewayofdoingthings"           call        system.string.op_equality           brfalse.s   notequal  **true branch**            ret  notequal:  **false branch**             ret 

so if want il rewriting conceivably intercept method calls, il signature looks ldstr, "...", call system.string.op_equality, brfalse.s , rewrite il modify code. however, approach extremely fragile. small differences in source code may generate il different signature , compiler writers know entire range of il signatures have for. if conditional statement involves several terms combined logical operators can become complicated determine insert instrumentation code. , there false positives. personally, not believe viable strategy instrumenting code.

however, have option. can compile source code using roslyn , use roslyn rewriters modify original source tree. aop @ source level opposed postsharp offers aop @ il level.

to rewrite syntax tree need derive class csharpsyntaxrewriter. rewriter modify if statements visitifstatement method overridden:

class ifstatementrewriter : csharpsyntaxrewriter {    public override syntaxnode visitifstatement(ifstatementsyntax ifstatement) {     var containsmagicstring = ifstatement       .condition       .descendantnodes()       .any(         syntaxnode => syntaxnode.tostring() == @"""somedumbstringbecausethisishorriblewayofdoingthings"""       );     if (!containsmagicstring)       // not modify if statement.       return ifstatement;     // @ "true" part , assume block (has curly braces).     var block = ifstatement.statement blocksyntax;     if (block == null)       // not modify if statement.       return ifstatement;     // insert instrumentation code @ start of block.     var instrumentationstatements = createinstrumentationstatements("true branch");     var modifiedstatements = block.statements.insertrange(0, instrumentationstatements);     var modifiedblock = block.withstatements(modifiedstatements);     return ifstatement.withstatement(modifiedblock);   }  } 

to insert instrumentation code system.console.writeline("true branch"); following rather hairy method used:

ienumerable<statementsyntax> createinstrumentationstatements(string text) {   return syntaxfactory.singletonlist<statementsyntax>(     syntaxfactory.expressionstatement(       syntaxfactory.invocationexpression(         syntaxfactory.memberaccessexpression(           syntaxkind.simplememberaccessexpression,           syntaxfactory.memberaccessexpression(             syntaxkind.simplememberaccessexpression,             syntaxfactory.identifiername("system"),             syntaxfactory.identifiername("console")           )           .withoperatortoken(syntaxfactory.token(syntaxkind.dottoken)),           syntaxfactory.identifiername("writeline")         )         .withoperatortoken(syntaxfactory.token(syntaxkind.dottoken))       )       .withargumentlist(         syntaxfactory.argumentlist(           syntaxfactory.separatedlist(             new[] {               syntaxfactory.argument(                 syntaxfactory.literalexpression(                   syntaxkind.stringliteralexpression,                   syntaxfactory.literal(text)                 )               )             }           )         )       )     )     .withsemicolontoken(syntaxfactory.token(syntaxkind.semicolontoken))   ); } 

you can modify syntax tree:

var root = (compilationunitsyntax) syntaxtree.getroot(); var rewriter = new ifstatementrewriter(); var rewrittenroot = rewriter.visit(root); 

you need expand code considerably better filter if statements , handle different ways if statements can written compared doing il rewriting should give higher chance of success.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -