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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:24:24+00:00 2026-05-20T09:24:24+00:00

Here is my attribute definition: [AttributeUsage(AttributeTargets.Field)] public class Optional : System.Attribute { public Optional()

  • 0

Here is my attribute definition:

[AttributeUsage(AttributeTargets.Field)]
public class Optional : System.Attribute
{
    public Optional()
    {
    }
}

In MyClass:

[Optional] public TextBox Name;

Finally in another function:

typeof(MyClass).GetFields().ToList<FieldInfo>().ForEach(x => writer.WriteLine(
   x.FieldType + " is called " + 
   x.Name + " and has attributes " + 
   x.GetCustomAttributes(true)[0]
 ));

The problem is I get an error for index 0. I just want to check for fields where the attribute is applied. The error goes away when I remove x.GetCustomAttributes(true)[0].

Exact Error:

Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.

Source Error:

Line 63:             }
Line 64: 
Line 65:             typeof(T).GetFields().ToList<FieldInfo>().ForEach(x => writer.WriteLine(x.FieldType + " is called " + 
Line 66:                 x.Name + " and has attributes " + 
Line 67:                 x.GetCustomAttributes(true)[0]+ "</br>"));
  • 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-20T09:24:25+00:00Added an answer on May 20, 2026 at 9:24 am

    Seems like there might be two questions here. To find all the fields that have your [Optional] attribute, you want:

    typeof(MyClass).GetFields().Where(
       f => f.GetCustomAttributes(typeof(OptionalAttribute), true).Any())
    

    To write out the custom attributes on all your fields, you want:

    typeof(MyClass).ToList<FieldInfo>().ForEach(x => 
    {
     writer.WriteLine(
       x.FieldType + " is called " + 
       x.Name + " and has attributes " + 
       string.Join(", ", x.GetCustomAttributes(true).Select(a => a.ToString()).ToArray()));
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My class definition: [Serializable] public class MyClass { [XmlAttribute(AttributeName = ID)] //Problem is here.
Is it possible to assign a System.Diagnostics.DebuggerDisplay attribute to the definition of a Dictionary?
I need to specify String constant as value of attribute: <int:header name=importedFilename /> here
Here is my problem I have arraycollection with the attribute Month with similar data
I have a query here regarding the value given to the update attribute of
As discussed here , C# doesn't support generic attribute declaration. So, I'm not allowed
I'm trying to find the value of a particular model's attribute in rails. Here's
So here's the output of inspect on a class: <Recurly::BillingInfo::CreditCard:0x1036a8a98 @prefix_options={}, @attributes={month=>1, last_four=>1, type=>bogus,
For a class marked with the attribute, [DataContract], does its private fields, which should
I have a class definition in which I want to alias several of its

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.