C# 自定义属性-缺少布尔参数默认为 false?

发布于 2024-11-15 00:24:50 字数 1241 浏览 4 评论 0原文

我有一个带有自定义属性的字段,如下所示:

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

我有一些查找此属性的代码,如下所示:

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.FieldType;
        }
        if (attribute.AllowNulls != null)
        {
            currentDefaultAllowNulls = attribute.AllowNulls;
        }
...

值 createtabledefaultallownulls 来自配置文件并设置为 true。然而,我发现当我在属性中省略“allownulls”时,它会用 false 值覆盖我的“true”默认值。

如果我在属性中仅指定 FieldType,则我预计“if (attribute.AllowNulls != null)”的计算结果为 false。相反,它表示该值不为 null,并表示 attribute.AllowNulls 为 false。因此,即使没有指定值,它也会覆盖我的默认值。这与布尔值不可为空有关吗?或者,如果您添加一个属性,您必须明确定义其中的所有参数?

我正在考虑的解决方法只是将其设为字符串并对其进行 bool.parsing,但这听起来不太优雅!有更好的办法吗?

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.FieldType;
        }
        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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混吃等死 2024-11-22 00:24:50

您需要除 true 或 false 之外的第三种状态,并且该状态将被取消设置。

将 AllowNulls 更改为 可空布尔值,布尔?。然后,AllowNulls 将允许 null、true 或 false 值。如果用户没有在属性中指定它,它将被初始化为 null。因此,您的 attribute.AllowNulls != null 将完全按照您的要求执行,并且用户仍然可以将该值设置为 truefalse

好的,所以您不能在属性上使用可空值。不知道。您可以使用如下所示的内容,而不是更改为字符串,尽管它不是那么漂亮:

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

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; }
苍暮颜 2024-11-22 00:24:50

除非我误解了你的问题,否则它总是错误的,除非你以其他方式初始化它。由于 bool 是一个值,因此它的默认值为 false。因此,如果没有初始化,它将保持这种状态。另外,attribute.AllowNulls != null 不应编译,因为值类型永远不能为 null。 Null 是为引用类型保留的,因为它比较的是引用本身以进行检查。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文