views:

131

answers:

2

Hi. Given the following classes, how can i intercept Class1.SampleMethod's Value from SampleAttribute? Thanks

Public Class Class1
    <SampleAttribute()> _
    Public Function SampleMethod(ByVal Value As Integer) As Boolean
        Return True
    End Function
End Class

<AttributeUsage(AttributeTargets.Method)> _
Public Class SampleAttribute
    Inherits System.Attribute

    Private _Value As Integer

    Property Value() As Integer
        Get
            Return _Value
        End Get
        Set(ByVal value As Integer)
            _Value = value
        End Set
    End Property

    Public Sub New()

    End Sub
End Class


EDIT:

Given Andrew Hare's answer, maybe I'm trying to use the wrong construct. I have a long list of similar methods and i need to execute a set of operations every time one of them is called. I thought that attaching an attribute to each of them would be the most straight-forward solution. Any suggestion?

A: 

Edit:

It sounds like you are trying to do something via AOP. Perhaps something like PostSharp would be helpful - it would allow you to do what you want via attributes.


Given the types you posted try something like this:

Imports System
Imports System.Reflection

Class Program
    Private Shared Sub Main()
        Dim type As Type = GetType(Class1)
        Dim members As MemberInfo() = type.GetMembers()

        For Each member As MemberInfo In members
            Dim attributes As Object() = member.GetCustomAttributes(GetType(SampleAttribute), True)

            If attributes.Length > 0 Then
                ' this means that the current "member"
                ' has your custom attribute
            End If
        Next
    End Sub
End Class
Andrew Hare
A: 

If you're trying to model cross-cutting behavior (unrelated objects need to do similar/same things) then AOP is the way to go. I've used PostSharp to great effect, both compile and run-time weaving. It will basically inject code at compile (or run-) time into the compiled assemblies which will call your methods based on your definition.

Update
RE: PostSharp: There are dozens of good tutorials out there on codeplex. You'll need to build a new attribute that inherits from PostSharp.Laos.OnMethodBoundaryAspect. Overriding the methods from that base class will tell the post-compiler what code to insert during compilation. That tracing example on codeplex should show you everything you need to do.
EndUpdate

Another architectural pattern you might want to look at is observer (e.g. a quick google found this msdn article).

If those methods are actually object behavior in some related classes, then an observer could be notified of activity, then respond accordingly. The disadvantage of a publish-subscribe pattern like this is you need to register your list of dependents with the observer. If you just have a bunch of methods on classes, then the concept still somewhat holds true but isn't necessarily ideal.

Of course, the poor man/woman's approach is to just add a line of code at the end of all the methods ;-)

joshua.ewer
Thanks for you reply! Can you provide an example of how could I accomplish my task with PostSharp? Thanks in advance.
pistacchio