本文介绍了dotnet 核心中的 RealProxy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 C# 中的 AOP 使用命名空间 System.Runtime.Remoting.ProxiesSystem.Runtime.Remoting.Messaging.我正在尝试将我的应用程序从 .Net Framework 4.6 移植到 dnxcore/dotnet 核心.

I'm working with the namespaces System.Runtime.Remoting.Proxies and System.Runtime.Remoting.Messaging for AOP in C#.I'm trying to port my application from .Net Framework 4.6 to dnxcore/dotnet core.

Intellisense 说,这两个命名空间在我的框架版本 (netcoreapp1.0/dnxcore50) 中不可用.知道这两个命名空间是否会出现吗?或者知道如何使用 RealProxy 类获得 AOP?

Intellisense says, that these two namespaces are not available with my framework-vesion (netcoreapp1.0 / dnxcore50). Any idea if these two namespaces will appear? or any idea how to get the AOP like with the RealProxy-class?

我不想使用 3rd-party-libraries - 我只想使用 .Net 提供给我的东西.

I don't want to use 3rd-party-libraries - I only want to use what .Net offers me.

推荐答案

看起来 RealProxy 赢了来到 .NET Core/Standard.在该问题中,一位 Microsoft 开发人员建议将 DispatchProxy 作为替代.

It looks like RealProxy won't come to .NET Core/Standard. In the issue, a Microsoft developer suggests DispatchProxy as an alternative.

此外,一些现有的 AOP 框架可能已经或将来支持 .NET Core(如对问题的评论所示).

Also, some existing AOP frameworks may support .NET Core already or in the future (as seen in the comments on the question).

另一种选择是 DispatchProxy,这里有一个很好的例子:http://www.c-sharpcorner.com/article/aspect-orientation-programming-in-c-sharp-using-dispatchproxy/.

An alternative is the DispatchProxy, which has a wonderful example here: http://www.c-sharpcorner.com/article/aspect-oriented-programming-in-c-sharp-using-dispatchproxy/.

如果我们简化代码,这就是我们得到的:

If we simplify the code, this is what we get:

public class LoggingDecorator<T> : DispatchProxy
{
    private T _decorated;

    protected override object Invoke(MethodInfo targetMethod, object[] args)
    {
        try
        {
            LogBefore(targetMethod, args);

            var result = targetMethod.Invoke(_decorated, args);

            LogAfter(targetMethod, args, result);
            return result;
        }
        catch (Exception ex) when (ex is TargetInvocationException)
        {
            LogException(ex.InnerException ?? ex, targetMethod);
            throw ex.InnerException ?? ex;
        }
    }

    public static T Create(T decorated)
    {
        object proxy = Create<T, LoggingDecorator<T>>();
        ((LoggingDecorator<T>)proxy).SetParameters(decorated);

        return (T)proxy;
    }

    private void SetParameters(T decorated)
    {
        if (decorated == null)
        {
            throw new ArgumentNullException(nameof(decorated));
        }
        _decorated = decorated;
    }

    private void LogException(Exception exception, MethodInfo methodInfo = null)
    {
        Console.WriteLine($"Class {_decorated.GetType().FullName}, Method {methodInfo.Name} threw exception:
{exception}");
    }

    private void LogAfter(MethodInfo methodInfo, object[] args, object result)
    {
        Console.WriteLine($"Class {_decorated.GetType().FullName}, Method {methodInfo.Name} executed, Output: {result}");
    }

    private void LogBefore(MethodInfo methodInfo, object[] args)
    {
        Console.WriteLine($"Class {_decorated.GetType().FullName}, Method {methodInfo.Name} is executing");
    }
}

因此,如果我们有一个带有相应接口(此处未显示)的示例类 Calculator:

So if we have an example class Calculator with a corresponding interface (not shown here):

public class Calculator : ICalculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

我们可以像这样简单地使用它

we can simply use it like this

static void Main(string[] args)
{
    var decoratedCalculator = LoggingDecorator<ICalculator>.Create(new Calculator());
    decoratedCalculator.Add(3, 5);
    Console.ReadKey();
}

这篇关于dotnet 核心中的 RealProxy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 04:41