2010-12-14 8 views
0

メソッドからカスタム属性を取得するのに苦労しています。ご覧のとおり、ProcessXMLメソッドにはカスタム属性があります。メソッド自体は匿名のデリゲートに渡され、そのコンテキストでカスタム属性を取得しようとしていますが、どうすればよいかわかりません。匿名デリゲートのGetCustomAttributes

誰かが私にそれを行う適切な方法を示すことができたら本当に感謝しています。多くのおかげで

はここだから私はAOP(Postsharpは)私がここでやろうとしたものachieveingへのよりよい方法だったことに気づいた私のコード

Public Sub UpdateXML() 

     Dim errors = New Errors 

     Dim xml = <testxml> 
         <element value="1"/> 
         <element value="2"/> 
         <element value="3"/> 
        </testxml> 

     Dim funcWithErrorTrapping = ProcessXML().WithErrorTrapping(errors) 

     Dim res = funcWithErrorTrapping(xml) 
     If errors.Count = 0 Then 
      MessageBox.Show("Success - " & res) 
     Else 
      MessageBox.Show("Failure - " & errors.Count) 
     End If 

    End Sub 

    <ThrowException(123456, "He's a very naughty boy")> 
    Private Overloads Function ProcessXML() As Func(Of XElement, String) 

     Return Function(x) 
        For Each e In x.Descendants("element") 
         e.Attribute("value").Value = "set" 
        Next 

        Throw New Exception 

        Return x.ToString 

       End Function 

    End Function 
End Class 

Public Class Errors 
    Inherits Dictionary(Of Integer, String) 
End Class 

<AttributeUsage(AttributeTargets.All, inherited:=False, AllowMultiple:=False)> 
Public NotInheritable Class ThrowException 
    Inherits Attribute 

    Private _number As Integer 
    Private _description As String 

    Public Sub New(ByVal number As Integer, ByVal description As String) 
     _number = number 
     _description = description 
    End Sub 

    Public ReadOnly Property Number As Integer 
     Get 
      Return _number 
     End Get 
    End Property 

    Public ReadOnly Property Description As String 
     Get 
      Return _description 
     End Get 
    End Property 

End Class 

Public Module Extensions 

    <Extension()> 
    Public Function WithErrorTrapping(Of T, T1)(ByVal process As Func(Of T, T1), ByVal errors As Errors) As Func(Of T, T1) 

     Return Function(a) 
        Try 
         Return process.Invoke(a) 
        Catch ex As Exception 
         Dim [exception] = process.Method.GetAttributes(Of ThrowException)()(1) 
         errors.Add([exception].Number, [exception].Description) 
        End Try 
       End Function 

    End Function 

    <Extension()> 
    Public Function GetAttributes(Of T As Attribute)(ByVal attributesProvider As ICustomAttributeProvider) 
     Dim attributes = New List(Of T) 
     For Each attr As Object In attributesProvider.GetCustomAttributes(GetType(T), False) 
      If TypeOf attr Is T Then 
       attributes.Add(TryCast(attr, T)) 
      End If 
     Next 

     Return attributes 

    End Function 

End Module 

答えて

1

です!