Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6241881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:50:54+00:00 2026-05-24T11:50:54+00:00

I am struggling to retrieve custom attributes from a method. As you can see,

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:50:55+00:00Added an answer on May 24, 2026 at 11:50 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently struggling with a query that needs to retrieve multiple records from
I'm working on an app that can retrieve images from the web. Rather than
A coworker has been struggling with this problem. The desired result is an installable
For the past 2 days I've been struggling trying to successfully authenticate users into
I'm using cursors to retrieve data from a database. I know how to retrieve
I've created an ISearchable interface that I've Typed so that I can retrieve an
I am building an iPhone app with MonoTouch and it gets its data from
I'm trying to retrieve all the data id from a database where their tags(array)
Struggling with an install of GAE-Patch and using my stylesheets. My settings.py has the
I´m struggling to understand this concept: I have a fixed size definition: (from http://msdn.microsoft.com/pt-br/library/aa931918.aspx

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.