This project is read-only.

SA1401 - Needs exception?

May 29, 2012 at 8:09 PM
Edited May 29, 2012 at 8:09 PM

SA1401 is great, and I'm glad we have the rule.

There's an exception though, and that's when you have public static readnly fields.

.Net guidelines encourage this over using const as the consuming app must be recompiled if using consts, or a suble bug may appear if the value of the const is later changed.  Static readonlys don't end up embedded in client IL.  Consts can, and if a library is updated but not the client, the const values won't change in the client code as they have the hardcoded values.

See here:

Would it be possible to change this rule to ignore static readonly fields?

May 29, 2012 at 8:19 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
May 30, 2012 at 10:48 AM
Edited May 30, 2012 at 10:49 AM

I am missing something? 1401 already ignores public readonly fields. The code is:

if (!nativeMethods || !privateOrInternal)
    Field field = (Field)element;
    if (!field.Const && !field.Readonly && !field.Generated)
        this.AddViolation(element, Rules.FieldsMustBePrivate);