开发者

c# custom attribute- missing boolean parameters default to false?

I have a field with a custom attribute on it, that looks like this:

[DBDataTypeAttribute(FieldType = "varchar(1000)",AllowNulls=false)]
public String ERLineID;

I've got some code that looks for this, like so:

foreach (FieldInfo field in userType.GetFields())
{
    currentDefaultDataType = createtabledefaultdatatype;
    currentDefaultAllowNulls = createtabledefaultallownulls;

    DBDataTypeAttribute attribute =
        (DBDataTypeAttribute)Attribute.GetCustomAttribute(field, typeof(DBDataTypeAttribute));

    if (attribute != null)
    {
        // at least one of createtabledefaultdatatype or createtabledefaultallownulls exists
        if (attribute.FieldType != null)
        {
            currentDefaultDataType = attribute.F开发者_如何学PythonieldType;
        }
        if (attribute.AllowNulls != null)
        {
            currentDefaultAllowNulls = attribute.AllowNulls;
        }
...

The value createtabledefaultallownulls comes from a config file and is set to true. However I've found when I leave off the 'allownulls' in my attribute, it overwrites my 'true' default with a value of false.

If I specified only the FieldType in my attribute, I would have expected 'if (attribute.AllowNulls != null)' to evaluate false. Instead, it says that the value isn't null and says that attribute.AllowNulls is false. So it overwrites my default even though no value was specified. Is this something to do with bools not being nullable? Or perhaps if you add an attribute you must explicity define all the parameters within it?

The workaround I'm considering is just making it a string and bool.parsing it, but that doesn't sound very elegant! Is there a better way?


You need a third state other than true of false, and that would be unset.

Change AllowNulls to a Nullable boolean, bool?. AllowNulls will then allow the values of null, true, or false. If the user does not specify it in the attribute it will be initialized to null. So your attribute.AllowNulls != null will do exactly what you want and users will still be able to set the value to true or false.

Ok, so you can't use nullables on Attributes. Didn't know that. You could use something like below rather than changing over to a string, although it isn't that pretty:

 private bool? allowNulls = null;
 public bool AllowNulls  {
    get { return allowNulls.GetValueOrDefault(false); }
    set { allowNulls = value; }
 }
 public bool AllowNullsWasSet() { return allowNulls.HasValue; }


Unless I am misunderstanding your question, it will always be false unless you initialize it otherwise. Since bool is a value, it has a default value of false. So without initialization, it will remain that way. Also, attribute.AllowNulls != null should not compile, since the value type can never be null. Null is reserved for reference types, since it is the reference itself that it compares for its check.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜