Search code examples
vb.netvariablespostsharpaop

How to set a property when calling an Aspect in VB.NET with PostSharp


Trying to set my property for my Aspect `made with postSharp librarry in VB .net. How can I do it?

Here is my Aspect:

<Serializable()>
Public Class MyAspect
    Inherits OnMethodBoundaryAspect

    Public Property CurrentTool As String

    Public Overrides Sub OnEntry(ByVal eventArgs As PostSharp.Aspects.MethodExecutionArgs)
        MyBase.OnEntry(eventArgs)
        eventArgs.MethodExecutionTag = Stopwatch.StartNew()
    End Sub

    Public Overrides Sub OnExit(ByVal eventArgs As PostSharp.Aspects.MethodExecutionArgs)
        MyBase.OnExit(eventArgs)
        Dim sw As Stopwatch = CType(eventArgs.MethodExecutionTag, Stopwatch)
        sw.Stop()

        Log.log("Method: " & eventArgs.Instance.GetType().Name & "." & eventArgs.Method.Name & " - Total time:" + CStr(sw.ElapsedMilliseconds / 1000), " seconds.")

    End Sub


End Class

How do I call it before my method to be able to specify my CurrentTool property? Because this code below doesn't work..

 <MyAspect(CurrentTool = "LOGIN")>
Private Sub CallTologInFeature()
    ...
End Sub

Solution

  • When setting an attribute's property value, you need to use := operator.

    <MyAspect(CurrentTool:="LOGIN")>
    Private Sub CallTologInFeature()
    ...
    End Sub