PC Review


Reply
Thread Tools Rate Thread

custom attributes

 
 
Random
Guest
Posts: n/a
 
      14th Feb 2007
I have a class with methods that I am applying custom attributes to. Right
now, I'm using reflection within each method to check for the attribute and
do something if it exists and depending on it's settings. I'd like to
automate that so the class will call a helper method by itself that doees
this check so that no code has to be written within each method (otherwise
what's the point if the developer has to write the code to check for the
attribute and it's values).

Example:

<EmailAttribute("sendurgent")> _
Public Sub UpdatePersonnelRecord(ByVal personnelID as Integer)
(do operations to perform data operation on personnel)

Dim oAttr As EmailAttribute = GetEmailAttribute()
'perform operation depending on the values set in the attribute
End Sub

Protected Function GetEmailAttribute() As EmailAttribute
Dim oAttr() As CustomAttributes.EmailAttribute
Dim oCAttr As CustomAttributes.EmailAttribute
Dim method As MethodBase = New StackFrame(1, False).GetMethod()

oAttr = method.GetCustomAttributes(GetType(EmailAttribute), True)
If UBound(oAttr) = -1 Then
'do nothing, attribute does not exist
Return Nothing
Else
oCAttr = oAttr(0)
Return oCAttr
End If
End Function

-----
What I'd like to do is to be able to get rid of the lines in the
UpdatePersonnelRecord method...

Dim oAttr As EmailAttribute = GetEmailAttribute()
'perform operation depending on the values set in the attribute

....so they will be done automatically


 
Reply With Quote
 
 
 
 
Oliver Sturm
Guest
Posts: n/a
 
      14th Feb 2007
Hello Random,

>What I'd like to do is to be able to get rid of the lines in the
>UpdatePersonnelRecord method...
>
>Dim oAttr As EmailAttribute = GetEmailAttribute()
>'perform operation depending on the values set in the attribute
>
>...so they will be done automatically


Sorry, I don't understand how this could ever be done "automatically" -
after all, if you want to handle the value assigned to the attribute in
your method, you'll always need to have at least one line that pulls said
value into a local variable. Right?

I might be misunderstanding something here, as your whole example is not
entirely clear to me... after all, an attribute is always a value that is
static at compile time. I don't understand why I would apply such an
attribute to a method, thereby specifying a value that I could just as
well pass in to the method using a normal parameter...


Oliver Sturm
--
http://www.sturmnet.org/blog
 
Reply With Quote
 
Random
Guest
Posts: n/a
 
      14th Feb 2007
The idea is that the attribute would be applicable to any method in any
class, and would be static for that method. It standardizes the developer's
job of attaching that information to the method. Any application that uses
the class would not pass any parameters to the method to influence it's
behavior. The application would be "blind" to it's existence.

Trying to code the behavior that results from the attribute being applied to
the method is what I'm trying to automate.

"Oliver Sturm" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello Random,
>
>>What I'd like to do is to be able to get rid of the lines in the
>>UpdatePersonnelRecord method...
>>
>>Dim oAttr As EmailAttribute = GetEmailAttribute()
>>'perform operation depending on the values set in the attribute
>>
>>...so they will be done automatically

>
> Sorry, I don't understand how this could ever be done "automatically" -
> after all, if you want to handle the value assigned to the attribute in
> your method, you'll always need to have at least one line that pulls said
> value into a local variable. Right?
>
> I might be misunderstanding something here, as your whole example is not
> entirely clear to me... after all, an attribute is always a value that is
> static at compile time. I don't understand why I would apply such an
> attribute to a method, thereby specifying a value that I could just as
> well pass in to the method using a normal parameter...
>
>
> Oliver Sturm
> --
> http://www.sturmnet.org/blog



 
Reply With Quote
 
Oliver Sturm
Guest
Posts: n/a
 
      14th Feb 2007
Hello Random,

I'm sorry - I may be dense, but I really can't imagine what this would be
good for. Can you describe a practical example of how this is going to be
used once it's finished?

Plus, you don't answer part one of my previous question - what do you want
to "automate"? As I said, if you're going to handle the value from the
attribute inside the method, you'll need to have at least that one line
that pulls that value into a local variable. What do you imagine how you
could get rid of that single line?


Oliver Sturm
--
http://www.sturmnet.org/blog
 
Reply With Quote
 
Random
Guest
Posts: n/a
 
      14th Feb 2007
Well, Oliver, that's why I'm posting here. I'm trying to see how it could
be done to eliminate those lines of code inside the method.

As for practicality, the example given could be used to attach the attribute
to any number of methods that each update a part of an employee's profile.
The attribute could describe the method of notification that needs to be
sent out, the urgency, the format... any number of things (the example I
posted only uses one property for the attribute). The attribute itself has
nothing to do with the main intent of the method, to do the update to the
record, it only "attaches" some other behavior to the method.

I'm looking at doing maybe declarative event handling on a base class,
seeing if that will work.

"Oliver Sturm" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello Random,
>
> I'm sorry - I may be dense, but I really can't imagine what this would be
> good for. Can you describe a practical example of how this is going to be
> used once it's finished?
>
> Plus, you don't answer part one of my previous question - what do you want
> to "automate"? As I said, if you're going to handle the value from the
> attribute inside the method, you'll need to have at least that one line
> that pulls that value into a local variable. What do you imagine how you
> could get rid of that single line?
>
>
> Oliver Sturm
> --
> http://www.sturmnet.org/blog



 
Reply With Quote
 
Oliver Sturm
Guest
Posts: n/a
 
      15th Feb 2007
Hello Random,

>Well, Oliver, that's why I'm posting here. I'm trying to see how it could
>be done to eliminate those lines of code inside the method.


Your sample has only ever shown one single line of code in that method of
yours. I've told you in so many words that there's logically no way to get
rid of that line. I don't know what else I can say about this.

Sorry I can't help you, I hope somebody else can step forward.


Oliver Sturm
--
http://www.sturmnet.org/blog
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
custom attributes in .Net 2.0 =?Utf-8?B?d2luZHNpbQ==?= Microsoft C# .NET 1 24th May 2007 09:36 AM
WebControl.Attributes.Add and custom attributes P4trykx Microsoft ASP .NET 2 31st Jan 2007 05:33 PM
Help with custom attributes =?Utf-8?B?V2lsbCBXYWdnb25lcg==?= Microsoft ASP .NET 0 3rd Mar 2005 06:29 PM
about Custom Attributes! xiaorun huang Microsoft Dot NET Framework 1 25th Feb 2004 03:52 AM
Re: Add custom attributes to AD Russell greenwood Microsoft Windows 2000 Active Directory 1 25th Jul 2003 06:57 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:34 AM.