匿名委托上的 GetCustomAttributes

发布于 2024-10-07 17:52:45 字数 2907 浏览 3 评论 0原文

我正在努力从方法中检索自定义属性。正如您所看到的,ProcessXML 方法有一个自定义属性。该方法本身被传递到一个匿名委托中,然后,在这种情况下,我希望获取它的自定义属性,但我不确定如何做到这一点。

如果有人能告诉我正确的方法,我将非常感激。非常感谢

这是我的代码

    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

I am struggling to retrieve custom attributes from a method. As you can see, the method ProcessXML has a custom attribute. The method itself gets passed into an anonymous delegate and then, in that context, I'm looking to get its custom attributes, but I'm not sure quite how to do it.

I'd be really grateful if someone could show me the proper way to do it. Many thanks

Here's my code

    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幼儿园老大 2024-10-14 17:52:45

所以我意识到 AOP(Postsharp)是实现我在这里尝试做的事情的更好方法!

So I realised that AOP (Postsharp) was a much better approach to achieveing what I was trying to do here!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文