尝试在c#中打破管道时出现真正的代理实现问题

时间:2018-02-21 10:46:32

标签: c# proxy pipeline realproxy

AuthenticationProxy.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Reflection;
using System.Threading;

namespace ProxyWithAOP
{
public class AuthenticationProxy<T> : RealProxy
{
    private readonly T _decorated;
    public AuthenticationProxy(T decorated)
      : base(typeof(T))
    {
        _decorated = decorated;
    }

    private void Log(string msg, object arg = null)
    {
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine(msg, arg);
        Console.ResetColor();
    }

    public override IMessage Invoke(IMessage msg)
    {
        var methodCall = msg as IMethodCallMessage;
        var methodInfo = methodCall.MethodBase as MethodInfo;

        if (Thread.CurrentPrincipal.IsInRole("ADMIN"))
        {
            try
            {
                Log("User authenticated - You can execute '{0}' ", methodCall.MethodName);

                var result = methodInfo.Invoke(_decorated, methodCall.InArgs);

                return new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall);
            }
            catch (Exception e)
            {
                Log(string.Format("User authenticated - Exception {0} executing '{1}'", e), methodCall.MethodName);

                return new ReturnMessage(e, methodCall);
            }
        }

        Log("User not authenticated - You can't execute '{0}' ", methodCall.MethodName);

        return new ReturnMessage(-1, null, 0, methodCall.LogicalCallContext, methodCall);
    }
}
}

DynamicProxy.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ProxyWithAOP
{
class DynamicProxy<T> : RealProxy
{
    private readonly T _decorated;

    public DynamicProxy(T decorated)
      : base(typeof(T))
    {
        _decorated = decorated;
    }

    private void Log(string msg, object arg = null)
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine(msg, arg);
        Console.ResetColor();
    }

    public override IMessage Invoke(IMessage msg)
    {
        var methodCall = msg as IMethodCallMessage;
        var methodInfo = methodCall.MethodBase as MethodInfo;

        Log("In Dynamic Proxy - Before executing '{0}'", methodCall.MethodName);

        try
        {
            var result = methodInfo.Invoke(_decorated, methodCall.InArgs);

            Log("In Dynamic Proxy - After executing '{0}' ", methodCall.MethodName);

            return new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall);
        }
        catch (Exception e)
        {
            Log(string.Format("In Dynamic Proxy- Exception {0} executing '{1}'", e), methodCall.MethodName);

            return new ReturnMessage(e, methodCall);
        }
    }
}
}

我已将两种方法声明为 - int GetCustomer(T entity);double GetBalance(T entity);

在管道中添加方法

var decorated = TraceProxy<ICustomerOperations>.Create(new CustomerOperations());
decorated = AuthenticationProxy<ICustomerOperations>.Create(decorated);

现在我使用装饰器调用我的客户方法

decorated.GetCustomer(1);
decorated.GetBalance(1);

上面的方法有不同的返回类型,如int和string 当用户未在AuthenticationProxy类中进行身份验证时,它将对象返回为return new ReturnMessage(-1, null, 0, methodCall.LogicalCallContext, methodCall);(Authentication.cs调用方法)

decorated.GetCustomer(1);调用已成功,因为返回类型匹配为int。但是,decorated.GetBalance(1);会将InvalidCastException异常作为其超出double返回类型的值。{/ p>

那么如何解决这个问题呢?我想让它变得通用,以便不存在返回类型问题。

0 个答案:

没有答案