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

  • Home
  • SEARCH
  • 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 244621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:01:12+00:00 2026-05-11T21:01:12+00:00

I don’t really understand attributes. I’ve read all kinds of books & posts on

  • 0

I don’t really understand attributes. I’ve read all kinds of books & posts on them, but I just don’t get it.

Since I don’t understand them, I also don’t understand how to effectively use them.

1) Can you give me a good definition of what an attribute is & what it’s used for?

2) Can you give me a good code example in C# of how to make and consume a custom attribute?

  • 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-11T21:01:12+00:00Added an answer on May 11, 2026 at 9:01 pm

    Say you’ve got a class with a series of properties that you are going to walk through using reflection. Any that are strings may need to be validated to check they are not longer than a certain amount.

    You could then create a TextLength attribute, with a default integer constructor and integer property/field. You could then read your attribute on each string property in your class and compare the length of the property value to the number specified in the attribute.

    Code:

    public class TextLengthAttribute : Attribute
    {
        private int length;
        public int Length { get { return length; } set { length = value; } }
    
        public TextLengthAttribute(int num) { this.length = num; }
    }
    
    public class MyClass
    {
        [TextLength(10)]
        public string Property1;
        [TextLength(20)]
        public string Property2;
    }
    
    public class ClassReader
    {
         public static void Main()
         {
              MyClass example = MyClass.GetTestData();
    
              PropertyInfo[] props = typeof(MyClass).GetProperties();
              foreach (PropertyInfo prop in props)
              {
                   if (prop.ValueType == typeof(String) 
                   {
                        TextLengthAttribute[] atts = 
                          (TextLengthAttribute)[]prop.GetCustomAttributes(
                               typeof(TextLengthAttribute), false);
                        if (prop.GetValue(example, null).ToString().Length > 
                             atts[0].Length) 
                            throw new Exception(prop.name + " was too long");
                   }
              }
         }
    }
    

    Note: untested

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

Sidebar

Related Questions

Don't get me wrong, I want them to get saved. But I always thought
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to explain it better but i'm trying to get a response
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't ask me how but I managed to get accidentally the following remote branches
don't understand: in my controller: @json = User.all.to_gmaps4rails do |user| \Title\: \#{user.email}\ end in
I don't get to see the source code but the .class file. Can I
Don't get me wrong - I love Smalltalk, but... To me, the Squeak interface
Don't know whether I'm having a "thick day" - but I just wondered what
don't know if the title describes anything about what I'm trying to say but

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.