2

I'm trying to implement an OnMethodBoundary aspect on an abstract method in an abstract class so that all types that inherit from this class will automatically have the aspect applied. There are no compilation errors or warnings, but the OnEntry method doesn't fire. Note: If I apply the aspect to a non-abstract method, everything works fine

here's the aspect example:


    [Serializable]
    [MulticastAttributeUsage(MulticastTargets.Method, Inheritance = MulticastInheritance.Multicast)]
    public sealed class DoSomethingAttribute : OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionArgs args)
        {
            //Do work
        }
    }

// here's the abstract class

public abstract class Job
    {
        //...
        [DoSomething]
        public abstract void Run();
    }
Marcus
  • 111
  • 1
  • 1
  • 10

1 Answers1

2

Updated answer: it doesn't matter where anything is, as long as both projects have Postsharp referenced then you're good to go.

It works just fine. Which version of PostSharp are you using?

class Program
{
    static void Main(string[] args)
    {
        Job1 j = new Job1();
        j.Run();
        Console.ReadKey();
    }
}

[Serializable]
[MulticastAttributeUsage(MulticastTargets.Method, Inheritance = MulticastInheritance.Multicast)]
public sealed class DoSomethingAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry(MethodExecutionArgs args)
    {
        Console.WriteLine("OnEntry");
    }
}

public abstract class Job
{
    //...
    [DoSomething]
    public abstract void Run();
}

public class Job1 : Job
{

    public override void Run()
    {
        Console.WriteLine("Run method");
    }
}

Results:

OnEntry 
Run method
ekkis
  • 9,804
  • 13
  • 55
  • 105
ILovePaperTowels
  • 1,445
  • 2
  • 17
  • 30
  • Thanks for the Reply. I'm using version 2.1.6.4. – Marcus Feb 24 '12 at 17:03
  • Ok so the problem is that my Instance class (Job1) lives in a different assembly. Console app & Job1 in one assembly, abstract class, and aspect in another assembly. Any thoughts on how I fix that? (i can't move the code around because its in different assemblies for a specific reason) – Marcus Feb 24 '12 at 17:05
  • @Marcus it doesn't matter where anything is, as long as both projects have Postsharp referenced then you're good to go. – ILovePaperTowels Feb 24 '12 at 17:25
  • 2ILovePaperTowels. Thanks, that was the problem. I wasn't referencing PostSharp in my 2nd assembly. I figured there would be some compile-time checking for those types of mistakes.Could you put that in an answer instead of a comment so I can select it as the 'Answer'. :) – Marcus Feb 27 '12 at 13:55
  • @Marcus glad you got it working. I've updated the answer to include my comment. – ILovePaperTowels Feb 27 '12 at 15:42