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 8007443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:49:34+00:00 2026-06-04T17:49:34+00:00

Good day, How can I get class’s property name, if I have custom attribute

  • 0

Good day, How can I get class’s property name, if I have custom attribute value for that property? and custom attribute name of course.

  • 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-06-04T17:49:36+00:00Added an answer on June 4, 2026 at 5:49 pm

    Get property name by custom attribute:

        public static string[] GetPropertyNameByCustomAttribute
          <ClassToAnalyse, AttributeTypeToFind>
          (
           Func<AttributeTypeToFind, bool> attributePredicate
          )
          where AttributeTypeToFind : Attribute
        {  
          if (attributePredicate == null)
          {
            throw new ArgumentNullException("attributePredicate");
          }
          else
          {
            List<string> propertyNames = new List<string>();
    
            foreach 
            (
              PropertyInfo propertyInfo in typeof(ClassToAnalyse).GetProperties()
            )
            {
              if
              (
                propertyInfo.GetCustomAttributes
                (
                  typeof(AttributeTypeToFind), true
                ).Any
                (
                  currentAttribute =>                  
                  attributePredicate((AttributeTypeToFind)currentAttribute)
                )
              )
              {
                propertyNames.Add(propertyInfo.Name);
              }
            }  
    
            return propertyNames.ToArray();
          }
        }
    

    Test fixtures:

    public class FooAttribute : Attribute
    {
      public String Description { get; set; }
    }
    
    class FooClass
    {
      private int fooProperty = 42;
    
      [Foo(Description="Foo attribute description")]
      public int FooProperty
      {
        get
        {
          return this.fooProperty;
        }
      }
    
    }
    

    Test cases:

    // It will return "FooProperty"
    GetPropertyNameByCustomAttribute<FooClass, FooAttribute>
    ( 
      attribute => attribute.Description == "Foo attribute description"
    );
    
    
    // It will return an empty array
    GetPropertyNameByCustomAttribute<FooClass, FooAttribute>
    ( 
      attribute => attribute.Description == "Bar attribute description"
    );   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day, I have found a strange quirk in Vim that I can't explain
Good day all, I have a class and a property, and I have three
Good day. I can handle a ComplexType such as: <xsd:element name=Prerequisite> <xsd:complexType> <xsd:sequence> <xsd:element
I have some input fields like below: <input type=text name=date_1 class=dataList value=2009-12-30 /> <input
Good Day, I have a number of binary strings that were created by a
Good day, i have a meta like this <meta name=viewport content=initial-scale=1.0, maximum-scale=1.0, user-scalabale=no, width=device-width,
Good day, masters. Suppose I have a java class A: class A { public
Good day, I have this problem with Html.DropDownListFor which I can't seem to work
Good day, I have a requirement to create a kind of book app with
Good day! i have a code below: def initial(*args): for arg in args: with

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.