Question about attributes...

  • Thread starter Thread starter Girish
  • Start date Start date
G

Girish

If I have something like that

class TestNegativeValue:Attribute
{
// ...
}
....
[TestNegativeValue]
public void ExceptionNegativeValue(int i)
{
//...
}

how can I test i(throw exception if i<0) directly
from the attribute body ?

Thanks.
Girish
 
Girish said:
If I have something like that

class TestNegativeValue:Attribute
{
// ...
}
...
[TestNegativeValue]
public void ExceptionNegativeValue(int i)
{
//...
}

how can I test i(throw exception if i<0) directly
from the attribute body ?

You cannot in normal circumstances.
You can implement a ContextAttribute on a ContextBoundObject, which will
allow you to analyze the messages entering and exiting a contexual object,
but it is extremely expensive for what you are doing, fairly difficult, and
has a questionable future life. I also don't know for sure how exceptions
work with it. I would avoid that if at all possible.

Another option is a product, with free and commercial versions you'll have
to evaluate yourself, XC#[1], which allows authors to write attributes to
modify and declare constraints using attributes. However, while the code
written for this compiler will compile correctly on any other C# compiler,
the attributes and constraints will only work on the XC# compiler and any
compiler which implements its functionality(which the MS and Mono C#
compilers do not currently do).

1. http://www.resolvecorp.com/
 
Daniel O'Connell said:
You cannot in normal circumstances.
You can implement a ContextAttribute on a ContextBoundObject, which will
allow you to analyze the messages entering and exiting a contexual object,
but it is extremely expensive for what you are doing, fairly difficult, and
has a questionable future life. I also don't know for sure how exceptions
work with it. I would avoid that if at all possible.

this is some really neat stuff. enables you to do AOP. add things like pre
post conditions, logging, transaction (not COM+ MTS transaction) all through
use of attribute markup.
 
Daniel Jin said:
this is some really neat stuff. enables you to do AOP. add things like
pre
post conditions, logging, transaction (not COM+ MTS transaction) all
through
use of attribute markup.

It has its uses, many of which are interesting, however it is depressingly
difficult to do correctly and troubling with regards to support. I find
myself worrying about how long it'll be before the layout or behaviour
changes.
 
Daniel O'Connell said:
It has its uses, many of which are interesting, however it is depressingly
difficult to do correctly and troubling with regards to support. I find
myself worrying about how long it'll be before the layout or behaviour
changes.

such is the risk of using undocumented api.

I have a question, how would you implement something like two phase commit
using this (if it's possible). since context is object bound rather than
method bound, I can't seem to see an obvious way to do this.
 
Back
Top