1

I have the below code which I'm using to try to apply custom attributes to fields in a declared class. I get the error below against the words 'FileType' and 'AllowNulls' in the declaration (noted below)

Error 3 'FieldType' is not a valid named attribute argument. Named attribute arguments must be fields which are not readonly, static, or const, or read-write properties which are public and not static. F:\Dropbox\Dev_LN Projects\02 Scrap\TestFieldAttributes\TestFieldAttributes\Program.cs 61 34 TestFieldAttributes

I've tried various combinations of removing static, readonly etc but no luck. Any thoughts? Thanks

[AttributeUsage(AttributeTargets.Field)]
public class DBDataTypeAttribute : System.Attribute
{
    private string fieldtype;
    public string FieldType
    {
        get { return fieldtype; }
    }

    private string allownulls;
    public string AllowNulls
    {
        get { return allownulls; }
    }

}

public class ExpenseReport
    {
        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        [DBDataTypeAttribute(FieldType = "varchar(1000)", AllowNulls = "true")]// errors on this line
        public String UniqueID;
        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public String ERNum;
    }

Thanks!

1 Answer 1

3

Your properties are read-only, and named arguments for an attribute must be read/write, so you must add a set method to the properties.

If you want to use read-only properties, you should use constructor arguments for the attribute rather than named arguments.

This means you could use either this:

[AttributeUsage(AttributeTargets.Field)]
public class DBDataTypeAttribute : System.Attribute
{
    private readonly string _fieldType;
    private readonly bool _allowNulls;

    public DBDataTypeAttribute(string fieldType, bool allowNulls)
    {
        _fieldType = fieldType;
        _allowNulls = allowNulls;
    }

    public string FieldType 
    {
        get { return _fieldType; } 
    } 

    public bool AllowNulls
    { 
        get { return _allowNulls; }
    } 
}

Or this:

[AttributeUsage(AttributeTargets.Field)]
public class DBDataTypeAttribute : System.Attribute
{
    public string FieldType { get; set; }
    public bool AllowNulls { get; set; }
}

Note: I used automatic properties for the latter example, but not the former, because I prefer making the fields readonly too in an attribute (readonly fields cannot be modified except in the constructor). I also changed AllowNulls to a bool since I see no reason for it to be a string.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent! You are exactly right - I removed the readonly part but didn't add a 'set'. Thanks also for the alternative examples. Allownulls was a bool originally, I'd changed it to string to see if that was part of the problem. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.