1

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);
    }
}
}

动态代理.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返回类型值。

那么如何解决这个问题呢?我想让它通用,这样就不应该有返回类型问题。

4

0 回答 0