CompoundAspect and adding OnMethodInvocation

Do you program directly PostSharp.CodeModel or another low-level API and need help? Put your questions here and we will try to answer.

CompoundAspect and adding OnMethodInvocation

Postby ddignam on Wed Jul 02, 2008 1:46 pm

I've written a simple Compoundaspect that adds an OnMethodinvocation. In my simple test program:

Code: Select all
static void Main(string[] args)
{
   List<int> a = new List<int>();

   a.Add(42);

   Fred<int> b = new Fred<int>();

   b.Add(42);
}


Only the call to Fred<int>.Add is woven, the call to List<int>.Add is not. Both methods appear to have the OnMethodInvocation aspect added.

Code: Select all
[AttributeUsage(AttributeTargets.Assembly)]
[MulticastAttributeUsage(MulticastTargets.Assembly)]
public class apply : CompoundAspect
{
   private void WeaveAdd(Type t, LaosReflectionAspectCollection collection)
   {
      foreach (MethodInfo m in t.GetMethods())
      {
         if (m.Name == "Add")
         {
            collection.AddAspect(m, new Hook());
         }
      }
   }

   public override void ProvideAspects(object element, LaosReflectionAspectCollection collection)
   {
      Assembly assy = element as Assembly;

      foreach (Type t in assy.GetTypes())
      {
         if (t.Name.Contains("Fred"))
         {
            WeaveAdd(t, collection);
         }
      }
            
      foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
      {
         foreach (Type t in a.GetTypes())
         {
            if (t.ToString().Contains("Generic.List`1["))
            {
               WeaveAdd(t, collection);
            }
         }
      }
   }
}
You do not have the required permissions to view the files attached to this post.
ddignam
 
Posts: 16
Joined: Mon Apr 07, 2008 5:07 pm
Full Name: Daniel Dignam
Company: Solidworks

Re: CompoundAspect and adding OnMethodInvocation

Postby ddignam on Wed Jul 02, 2008 3:52 pm

If I do the weave via this approach

Code: Select all
[assembly: ConsoleApplication2.Hook(AttributeTargetAssemblies = "mscorlib", AttributeTargetTypes = "System.Collections.Generic.List*", AttributeTargetMembers = "Add")]


Then it works, so it can be achieved.

--
Dan
ddignam
 
Posts: 16
Joined: Mon Apr 07, 2008 5:07 pm
Full Name: Daniel Dignam
Company: Solidworks

Re: CompoundAspect and adding OnMethodInvocation

Postby gfraiteur on Wed Jul 02, 2008 5:05 pm

Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 673
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: CompoundAspect and adding OnMethodInvocation

Postby ddignam on Wed Jul 02, 2008 5:16 pm

gfraiteur wrote:This is indeed a bug.

http://www.postsharp.org/tracker/view.php?id=232


Thanks as ever for the great product and support!

I've also reported http://www.postsharp.org/tracker/view.php?id=231 which may be related. Applying an aspect to an external generic method can lead to a corrupt image being generated

--
Dan
ddignam
 
Posts: 16
Joined: Mon Apr 07, 2008 5:07 pm
Full Name: Daniel Dignam
Company: Solidworks

Re: CompoundAspect and adding OnMethodInvocation

Postby ddignam on Thu Jul 03, 2008 10:51 am

gfraiteur wrote:This is indeed a bug.

http://www.postsharp.org/tracker/view.php?id=232


Hi Gael,

I installed 393 and the OnMethodInvocation is now being weaved, but I get an InvalidProgramException at runtime

I've reported this, with a reproducer, as

http://www.postsharp.org/tracker/view.php?id=233

--
Dan
ddignam
 
Posts: 16
Joined: Mon Apr 07, 2008 5:07 pm
Full Name: Daniel Dignam
Company: Solidworks


Return to PostSharp Core


cron