简体   繁体   English

Postsharp第三方课

[英]Postsharp 3rd party class

I need decorate all method from class in 3rd party DLL. 我需要在第三方DLL中从类中装饰所有方法。 I use C# 5.0 and postsharp 3.1. 我使用C#5.0和postharp 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? 有些人想知道我可以使用什么或google? Thank you 谢谢

Let's say you created OnMethodBoundary aspect to add some custom code at the end of the method: 假设你创建了OnMethodBoundary方面 ,在方法的末尾添加一些自定义代码:

[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. 要将此方面应用于第三方程序集,可以将其应用于项目并将AttributeTargetAssemblies属性设置为第3方程序集的名称。 This will cause PostSharp to modify your assembly and decorate the calls to the 3-rd party assembly with your custom code. 这将导致PostSharp修改您的程序集并使用您的自定义代码修饰对第三方程序集的调用。

[assembly: MyTest(AttributeTargetAssemblies = "SomeLibrary")]

I guess that this would be a good case for Castle Dynamic Proxy . 我想这对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 (ie a run-time derived class). 如果第三方类没有被密封(因此,它们允许继承和目标方法或属性是多态的),您应该能够创建运行时代理(即运行时派生类)。

Finally, you'll create a factory method that would return proxied instances of the whole third-party classes. 最后,您将创建一个工厂方法,该方法将返回整个第三方类的代理实例。

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. PostSharp适用于CIL级别,因此可以使用命令行工具(postsharp.4.0-x86.exe)并将方面编织到几乎任何程序集中。

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): 配置文件是常规的PostSharp配置文件(如.pssln和.psproj):

http://doc.postsharp.net/configuration-schema 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. 编辑:作为旁注 - PostSharp没有正式支持这种情况 - 所以如果遇到任何问题,你就可以自己动手了。

You can use a technique that PostSharp documentation calls Method Interception . 您可以使用PostSharp文档调用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. 它的工作原理是修改你的代码 - 在你调用第三方代码的网站上插入额外的代码,而不是触及第三方代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM