insert a method with attribute?

Jan 21, 2011 at 6:38 AM

Greeting.

I would like to insert a method just like:

     [PexMethod]      

    public int add([PexAssumeUnderTest]Class1 target, int j)        

     {      

         int result = target.add(j);          

         return result;          

      }

Is there any way to achieve this?

Coordinator
Jan 21, 2011 at 2:23 PM

Certainly, make a mutable copy of the assembly you want to modify, then make the new method (using the mutable metadata model), insert it in the right place and write out the assembly.

Jan 21, 2011 at 2:59 PM

Thanks for the quick responding.  If just for inserting the method, I know how to do it. however, I don't know how to add the attribute for the method and parameter.

Coordinator
Jan 21, 2011 at 5:08 PM

Add a new CustomAtrribute object to the Attributes collections of the method and parameter, respectively.

Jan 21, 2011 at 5:21 PM

could you give me an example?

Jan 21, 2011 at 6:53 PM

I still have some problems about how to define a new CustomAttribute, is there any sample code for that? 

Coordinator
Jan 24, 2011 at 1:03 AM

Have look at Normalizer.cs in the MutableMetadataModel project for examples of how custom attributes are constructed and added to a code model.