5

I need decorate all method from class in 3rd party DLL. I use C# 5.0 and postsharp 3.1. Of course I can do something like this.

//In 3rd party library
class A
{
    public virtual int foo(string a) {}

    public virtual void foo2() {}
}

//In my
class B : A
{
    public override int foo(string a) {
        int result = base.foo(a);
        //Do something
        return result;
    }

    public override void foo2() {
        base.foo2();
        //Do something
    }
}

do something is always the same.
I do not want to copy all of method that is ugly. Some idea what can I use or google? Thank you

Pratik
  • 1,531
  • 3
  • 25
  • 57
  • Unless something has changed, PostSharp works at compile time, which means you need to recompile that assembly from its source code. If you can't do that, you're left with disassembly and manual hacking, or you can reimplement it. – Lasse V. Karlsen Jul 04 '14 at 14:49
  • And truly there is no other option how to wrap all method? – user3805826 Jul 04 '14 at 15:00
  • You could create shim-objects in your own project that mimicks the real objects in terms of methods, properties, just calling down to an underlying real object, and then do your stuff in the shim methods. But, this means you'll have to make sure you don't directly use the real objects anywhere, you'll have to only use the shim objects. – Lasse V. Karlsen Jul 04 '14 at 15:03
  • You could create shim-objects in your own project that mimicks the real objects in terms of methods, properties, just calling down to an underlying real object, and then do your stuff in the shim methods. But, this means you'll have to make sure you don't directly use the real objects anywhere, you'll have to only use the shim objects. @LasseV.Karlsen – user3805826 Jul 04 '14 at 15:07
  • mainly I have to write all methods as in the example in question. I understand you correctly? @LasseV.Karlsen – user3805826 Jul 04 '14 at 15:08

4 Answers4

3

Let's say you created OnMethodBoundary aspect to add some custom code at the end of the method:

[Serializable]
public class MyTestAttribute : OnMethodBoundaryAspect
{
    public override void OnSuccess(MethodExecutionArgs args)
    {
        // Do something.
    }
}

To apply this aspect to a 3-rd party assembly, you can apply it in your project and set AttributeTargetAssemblies property to the name of the 3-rd party assembly. This will cause PostSharp to modify your assembly and decorate the calls to the 3-rd party assembly with your custom code.

[assembly: MyTest(AttributeTargetAssemblies = "SomeLibrary")]
AlexD
  • 5,011
  • 2
  • 23
  • 34
1

I guess that this would be a good case for Castle Dynamic Proxy.

If third-party classes aren't sealed (thus, they allow inheritance and target methods or properties are polymorphic), you should be able to create a run-time proxy (i.e. a run-time derived class).

Finally, you'll create a factory method that would return proxied instances of the whole third-party classes.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
1

PostSharp works on CIL level and thus it is possible to take the command-line tool (postsharp.4.0-x86.exe) and weave aspects into almost any assembly.

It goes like this:

postsharp.4.0-x86 /X:MyDependency.PostSharp.config MyDependency.dll

The config file is regular PostSharp configuration file (like .pssln and .psproj):

http://doc.postsharp.net/configuration-schema

However, one needs to be careful about license to the third party library.

EDIT: As a sidenote - this scenario is NOT officially supported by PostSharp - so you are on your own if you run into any problems.

Daniel Balas
  • 1,805
  • 1
  • 15
  • 20
1

You can use a technique that PostSharp documentation calls Method Interception. It works by modifying your code - inserting extra code at sites where you call the 3rd party code, instead of touching the 3rd party code.

cynic
  • 5,305
  • 1
  • 24
  • 40
  • Method interception alone won't be enough in this case. If you need to intercept the method call, then that method itself has be modified by PostSharp. To intercept only the calls to the 3-rd code without touching the 3-rd party assembly, you can use AttributeTargetAssemblies property when applying the attribute. – AlexD Jul 09 '14 at 10:43