装飾されたメソッドのパラメーターを列挙して、それらのパラメーターに適用されたカスタム属性を取得して、特定の値を決定しようとしています。
インターセプターには次のものがあります。これは、使用しようとした 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 の属性が表示されないことを除いて、他のすべては正常に機能します。
メソッドにカスタム属性が表示されない理由はありますか? メソッドが実際の「呼び出しターゲット」ではないことに関係があると思いますが、これを回避する方法はありません。助けてください!