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
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