Search code examples
postsharp

Add PostSharp Aspect to System.IO namespace?


I am looking to add my aspect to the system.io namespace, I already searches lots of solutions which didn't do the trick but basically I want to handle exceptions and do some logging on the File class from System.IO namespace. Just like adding [MyAspect] to the top of the class..

Is there a way?


Solution

  • Let's consider aspect like this:

    [PSerializable]
    public class MyAspect : OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionArgs args)
        {
        }
    }
    

    You can apply this aspect to any 3rd assembly called from your project by applying MyAspect:

    [assembly: MyAspect(AttributeTargetAssemblies = "mscorlib", AttributeTargetTypes = "System.IO.*")]
    

    When you specify this assembly attribute then PostSharp decorates all calls from your project to methods from System.IO namespace in mscorlib assembly with MyAspect.