CastleProjectのDynamicProxy

CastleProjectのDynamicProxyを使ってみた。用途はなぜかプロファイリング。nProfはまともに動かないし、DevPartner Profiler Community Editionは2003にしか対応してない・・・。
とりあえずDynamicProxyでプロファイルしてみることにする。

Public Class IntercetedProxy
    Implements Castle.DynamicProxy.IInterceptor


    Private m_watchs As New Dictionary(Of String, Stopwatch)
    Private m_count As New Dictionary(Of String, Integer)

    Private m_type As Type
    Private m_generator As New Castle.DynamicProxy.ProxyGenerator
    Public Sub New(ByVal type As Type)
        m_type = type
    End Sub

    Public Function Create(ByVal target As Object) As Object
        Return m_generator.CreateClassProxy(m_type, Me, target)
    End Function

    Public Sub Write()

        For Each value As KeyValuePair(Of String, Stopwatch) In m_watchs
            Console.WriteLine(value.Key & vbTab & value.Value.ElapsedMilliseconds & vbTab & m_count(value.Key))
        Next
    End Sub
    Public Function Intercept(ByVal invocation As Castle.DynamicProxy.IInvocation, ByVal ParamArray args() As Object) As Object Implements Castle.DynamicProxy.IInterceptor.Intercept
        If Not m_watchs.ContainsKey(invocation.Method.Name) Then
            m_watchs.Add(invocation.Method.Name, New Stopwatch)
            m_count.Add(invocation.Method.Name, 0)
        End If
        m_watchs(invocation.Method.Name).Start()

        Dim ret As Object = invocation.Proceed(args)

        m_watchs(invocation.Method.Name).Stop()
        m_count(invocation.Method.Name) = m_count(invocation.Method.Name) + 1
        Return ret

    End Function
End Class

こんなクラス。まあまあ適当に結果が出てまいりました。