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>"));