custom attributes

  • Thread starter Thread starter Random
  • Start date Start date
R

Random

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
 
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
 
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.
 
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
 
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.
 
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
 
Back
Top