1

装飾されたメソッドのパラメーターを列挙して、それらのパラメーターに適用されたカスタム属性を取得して、特定の値を決定しようとしています。

インターセプターには次のものがあります。これは、使用しようとした 2 つの異なるメソッドを示しています。GetParameters の取得と列挙の両方ですが、1 つは IsDefined を使用し、もう 1 つは GetCustomAttributes を使用しています。

public void Intercept(IInvocation invocation)
{
    try
    {

        var parameters = invocation.Request.Method.GetParameters();
        for (int index = 0; index < parameters.Length; index++)
        {
            foreach (var attrs in parameters[index]
                .GetCustomAttributes(typeof(EmulatedUserAttribute), true))
            {

            }

        }

        foreach (var param in invocation.Request.Method.GetParameters())
        {
            if (param.IsDefined(typeof (EmulatedUserAttribute), false))
            {
                invocation.Request.Arguments[param.Position] = 12345;
            }

        }


        invocation.Proceed();
    }
    catch(Exception ex)
    {
        throw;
    }
}

私が探している属性は単純で、実装はありません:

public class EmulatedUserAttribute : Attribute { }

そして InterceptAttribute:

[AttributeUsage(AttributeTargets.Method)]
public class EmulateAttribute : InterceptAttribute
{
    public override IInterceptor CreateInterceptor(IProxyRequest request)
    {
        return request.Context.Kernel.Get<IEmulateUserInterceptor>();
    }
}

そして、私が傍受している方法:

[Emulate]
public virtual List<UserAssociation> GetAssociatedProviders([EmulatedUser] int userId)
{
    return _assocProvAccountRepo.GetAssociatedProviders(userId);
}

ご覧のとおり、userId を EmulatedUser 属性で装飾し、メソッドをインターセプター属性で装飾しました。userId の属性が表示されないことを除いて、他のすべては正常に機能します。

メソッドにカスタム属性が表示されない理由はありますか? メソッドが実際の「呼び出しターゲット」ではないことに関係があると思いますが、これを回避する方法はありません。助けてください!

4

1 に答える 1

1

ブランドン

このコードを試してください。私はそれをうまく機能させました。クラスを定義した方法は次のとおりです。

public class Interceptor : SimpleInterceptor
{
    protected override void BeforeInvoke(IInvocation invocation)
    {
        var invokedMethod = invocation.Request.Method;
        if (invokedMethod.IsDefined(typeof(EmulateAttribute), true))
        {
            var methodParameters = invokedMethod.GetParameters();
            for (int i = 0; i < methodParameters.Length; i++)
            {
                var param = methodParameters[i];
                if (param.IsDefined(typeof (EmulatedUserAttribute), true))
                {
                    invocation.Request.Arguments[i] = 5678;
                }
            }
        }
    }
}

public interface IIntercepted
{
    [Emulate]
    void InterceptedMethod([EmulatedUser] int userId);
}

public class Intercepted : IIntercepted
{
    [Emulate]
    public void InterceptedMethod([EmulatedUser] int userId)
    {
        Console.WriteLine("UserID: {0}", userId);
    }
}

IInterceptedの代わりに のインスタンスをリクエストしていますIntercepted。具象クラスをリクエストすると、傍受は機能しません。多分これはあなたを正しい道に導くことができます。

var kernel = new StandardKernel();
kernel.Bind<IIntercepted>().To<Intercepted>().Intercept().With<Interceptor>();

var target = kernel.Get<IIntercepted>();

target.InterceptedMethod(1234); // Outputs 5678
于 2014-03-18T15:40:56.010 に答える