1

I created a couple of custom attributes that I attach to my methods in the handlers. The custom attributes are more than mere 'taggers' like e.g. 'RequiresAuthenticationAttribute'. A simplified example:

[EnforceParam("Account")]

In my interceptor, that gets called for methods annotated with EnforceParam, I'd like to get access to the value "Account". What I'm currently doing for that is this:

public override bool BeforeExecute(IOperation operation)
{
    ReflectionBasedMethod method = (ReflectionBasedMethod)((MethodBasedOperation)operation).Method;
    MethodInfo methodInfo = method.MethodInfo;

For that to work, I had to add the 'Method' property to OpenRasta's ReflectionBasedMethod.

Can the same be accomplished without hacking OpenRasta (I'm on 2.0 btw)?

Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156

1 Answers1

4

That's the wrong question. What you're looking for is simply:

var attribute = operation.FindAttribute<EnforceParamAttribute>()

Downcasting is not supported and the operation should only reflect an operation and its inputs, on purpose. Do not downcast, it will break and your code is not guaranteed to work beyond one version that happens to use the IMethod API, which is going at some point to be rewritten / removed.

Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156
SerialSeb
  • 6,701
  • 24
  • 28
  • Awesome, Thanks. I went for FindAttributes (plural), as I can have multiple attributes of the same type. Which brought up an issue: When I have 2 EnforceParamAttributes on a method, BeforeExecute is called twice on the same interceptor, but as I don't the attribute instance on which BeforeExecute is called I will use FindAttributes to look at all attributes, so calling twice is not necessary. Any idea on how to get around this other than setting a flag "ranBefore" inside BeforeExecute? – Evgeniy Berezovsky Oct 04 '11 at 00:08
  • @EugeneBeresovksy: that's probably worth another question - and I'm sure serialseb will appreciate it if you accept his answer! – Jeremy McGee Oct 04 '11 at 01:45
  • There shouldn't be an interceptor executing more than once if you registered it only once, is that what you're seeing? – SerialSeb Oct 13 '11 at 04:43