【问题标题】:Ninject retrieve Custom Attributes on parameters inside interceptorNinject 检索拦截器内参数的自定义属性
【发布时间】:2014-03-18 14:14:46
【问题描述】:

我正在尝试枚举修饰方法的参数以检索应用于这些参数的自定义属性以确定特定值。

我的拦截器中有以下内容,其中显示了我尝试使用的两种不同方法,都检索和枚举 GetParameters,但一种使用 IsDefined,另一种使用 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);
}

如您所见,我用 EmulatedUser 属性修饰了 userId,用我的拦截器属性修饰了方法。除了我看不到 userId 上的属性之外,其他一切都很好。

任何想法为什么我看不到方法上的自定义属性?我猜这与方法不是实际的“调用目标”有关,但我看不出有什么办法可以解决这个问题。请帮忙!

【问题讨论】:

    标签: ninject ninject-extensions ninject-interception


    【解决方案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
    

    【讨论】:

    • 谢谢你,这正是我所需要的!专门为界面添加属性。知道为什么会这样吗?
    • 被拦截的“Service”(类型)是接口,而不是实现本身。如果接口没有属性,您将无法在拦截器中看到它们。您可以使用invocation.Request.Service进行调试确认,它会返回IIntercepted,而不是Intercepted
    猜你喜欢
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多