c# - Postsharp skips to decorate methods that are marked with CompilerGenerated attribute -


this trying accomplish: converting 2500 integration tests nunit mstest, can run them microsoft test manager/lab. tests need run on user interface thread of product working on or not succeed.

this problem: have created postsharp aspect automatically run mstest test methods initialize environment tests , run them on ui thread. works fine, except tests created specflow. specflow generates code behind classes marked system.runtime.compilerservices.compilergenerated attribute. , when class marked attribute, postsharp seems skip methods in it.

the aspect defined on assembly level. have tried use multicastattributes.compilergenerated attributes during registration not seem change behavior. when place aspect directly on method, works.

i using latest stable version of postsharp (currently 3.1.).

a sample aspect:

[serializable] public class myaspect : postsharp.aspects.onmethodboundaryaspect {     public override void onentry(postsharp.aspects.methodexecutionargs args)     {         console.writeline("starting {0}", args.method.name);     }      public override void onexit(postsharp.aspects.methodexecutionargs args)     {         console.writeline("completed {0}", args.method.name);     } } 

the code trying apply to:

[assembly: postsharptestaspects.myaspect( attributetargettypeattributes = multicastattributes.public | multicastattributes.anygeneration, attributetargetelements = multicasttargets.method, attributetargetmemberattributes = multicastattributes.public | multicastattributes.anygeneration)]

class program {     static void main(string[] args)     {         new mytestclass().mytestmethod();         console.writeline("press key exit...");          console.readkey();     } }  [system.runtime.compilerservices.compilergenerated] public class mytestclass {             public void mytestmethod()     {         console.writeline("executing mytestmethod..");     } } 

when removing compilergenerated attribute postsharp applies aspect.

my questions are: behavior design? bug? there workaround? maybe need apply multicastattributes differently in assembly attribute?

postsharp ignores types have [compilergenerated] attribute applied when performing multicasting of aspect attributes. functionality design , needed avoid application of aspects on types generated c# compiler. generated types represent implementation details of c# compiler , applying attributes default expose these implementation details user.

however, postsharp treats generated methods in non-generated types less restriction. these methods user expects apply aspects default. example, automatic property accessors marked [compilergenerated]. can control behavior setting flags multicastattributes.compilergenerated , multicastattributes.usergenerated on attributetargettypeattributes property of aspect.

if need apply aspect compiler-generated types, can implementing own aspect provider , applying on assembly level.

[multicastattributeusage(multicasttargets.assembly)] public class sampleaspectprovider : multicastattribute, iaspectprovider {     public ienumerable<aspectinstance> provideaspects(object targetelement)     {         var myaspect = new myaspect();         var assembly = (assembly) targetelement;          foreach (var type in assembly.gettypes())         {             if (/* type valid target */)             {                 foreach (var methodinfo in type.getmethods())                 {                     yield return new aspectinstance(methodinfo, myaspect);                 }             }         }     } } 

and apply in target assembly:

[assembly: sampleaspectprovider] 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -