473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Question about attributes...

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
Nov 16 '05 #1
4 1047
You can't....
Attributes allow you to place extra descriptive information into metadata
that can be extracted using runtime reflection services.
Attributes do not actually execute (to perform tasks like parameter checking
etc...)

I guess what you're trying to do is what XC# features.... check it out at
http://www.resolvecorp.com/default.aspx

Eran Kampf
http://www.ekampf.com
"Girish" <gi****@in-reality.com> wrote in message
news:OQ**************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #2

"Girish" <gi****@in-reality.com> wrote in message
news:OQ**************@TK2MSFTNGP11.phx.gbl...
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/
Nov 16 '05 #3

"Daniel Jin" <Da*******@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...


"Daniel O'Connell [C# MVP]" wrote:

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.


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.

Nov 16 '05 #4
"Daniel O'Connell [C# MVP]" wrote:
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.
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: SB | last post by:
I have an assignment where we are supposed to mimic a scheduler routine of an OS. We are supposed to schedule some processes, an each process has the following attributes... - process id -...
1
by: Tony Johansson | last post by:
Hello! I'm reading a book about C++ and there is something that I don't understand so I ask you. I have marked the section from the book that is of intertest by tagging it with BOOK START HERE...
6
by: Tim Mavers | last post by:
I have a series of database objects that represent things such as people, accounts, etc. I have a set of options (boolean) that I need to add to these objects. Normally I would just create a bit...
6
by: Developer | last post by:
Hello, I'm experimenting with and XmlTextReader sample, and don't understand how the attributes are being processed. Here's the input: <xsd:element name="AA" type="BB"...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
1
by: Marcel Hug | last post by:
Hello NG ! I have a tricky question about dataSet and dataTable. In my app I have 2 databases. In the first (called A1) database are the whole datas but I'm interesting in one table tbl1 (with 20...
11
by: Doru Roman | last post by:
Hello, I got a test and some of the questions were: A collection a files, which is the basic unit of .NET programming, is called: 1. package 2. assembly 3. library 4. copybook
7
by: Ryan | last post by:
I'm in the process of learning more about building my ASP.NET website to use my SQL datastore and am a bit confused about how ADO.NET works with ASP.NET. This Microsoft article implies that using...
28
by: Alan Isaac | last post by:
I have a class whose instances should only receive attribute assignments for attributes that were created at inititialization. If slots are not appropriate, what is the Pythonic design for this? ...
4
by: RobG | last post by:
I have always accessed attributes such as disabled using the DOM element property, however I was wondering about implementing a more generic function to get the values of attributes - which of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.