【问题标题】:Cannot convert type bool to int无法将类型 bool 转换为 int
【发布时间】:2015-11-24 17:29:08
【问题描述】:

我丢失了源代码,不得不从 DLL 进行反编译,并且在 LINQ to SQL DataContext 类的代码中收到以下错误。

错误 CS0030 无法将类型 'bool' 转换为 'int'

看起来 VS 生成的代码出于某种奇怪的原因使用 int 而不是 bool。请参阅下面的星号线。我应该怎么做才能解决这个铸造错误?在这个类中使用了一百多次。

    public int? OrderBy
    {
        get
        {
            return this._OrderBy;
        }
        set
        {
            int? nullable = this._OrderBy;
            int? nullable1 = value;
            **if ((nullable.GetValueOrDefault() != nullable1.GetValueOrDefault() ? 1 : (int)(nullable.HasValue != nullable1.HasValue)) != 0)**
            {
                this.SendPropertyChanging();
                this._OrderBy = value;
                this.SendPropertyChanged("OrderBy");
            }
        }
    }

编辑: 这个问题的答案是“获得更好的反编译器”。上面的代码是由 Telerik Just Decompile 生成的。 ILSpy 生成真正有意义的代码。在这种情况下:

    [Column(Storage = "_OrderBy", DbType = "Int", UpdateCheck = UpdateCheck.Never), DataMember(Order = 6)]
    public int? OrderBy
    {
        get
        {
            return this._OrderBy;
        }
        set
        {
            if (this._OrderBy != value)
            {
                this.SendPropertyChanging();
                this._OrderBy = value;
                this.SendPropertyChanged("OrderBy");
            }
        }
    }

这个问题可能应该被删除。在 cmets 中提供建议。

【问题讨论】:

  • 看来你只是想要if(this._OrderBy != value)

标签: c# .net linq linq-to-sql


【解决方案1】:

你可能想要这个:

if ((nullable.GetValueOrDefault() != nullable1.GetValueOrDefault()) || (nullable.HasValue != nullable1.HasValue))

这应该给出与您所拥有的结果相同的结果,但更具可读性。

【讨论】:

  • 问题出在(int)(nullable.HasValue != nullable1.HasValue)。这就是将bool 转换为int
【解决方案2】:

您不能直接将bool 转换为int

(int)(nullable.HasValue != nullable1.HasValue) //invalid

但你可以使用Convert.ToInt32:

Convert.ToInt32(nullable.HasValue != nullable1.HasValue) //valid

所以你的代码应该是:

if ((nullable.GetValueOrDefault() != nullable1.GetValueOrDefault() ? 1 : Convert.ToInt32(nullable.HasValue != nullable1.HasValue)) != 0)

但是,由于“解除”运算符,我认为您可以直接进行比较。来自 C# 规范:

对于等式运算符 == [and] != 如果操作数类型都是不可为空的值类型并且结果类型是 bool,则存在运算符的提升形式。提升的形式是通过添加一个单一的?每个操作数类型的修饰符。提升的运算符认为两个空值相等,一个空值不等于任何非空值。如果两个操作数都不为 null,则提升的运算符解开操作数并应用底层运算符来生成 bool 结果。

所以你可以使用:

if (nullable != nullable1)

这当然意味着您可以完全省略 nullablenullable1 变量并使用

if (this._OrderBy != value)

【讨论】:

  • 问题是他们用这个(int)(nullable.HasValue != nullable1.HasValue)bool 转换为int
  • 同意,因此我的You can't directly cast an int to a bool 更改为Convert.ToInt32
  • 不,问题是将bool 转换为int,而不是将int 转换为bool
  • 对不起@juharr,我误读了你写的内容,我在我的回答中将其描述回了前面(尽管修复是正确的?)。我现在已经更新它(希望)更清晰。
  • 是的,这看起来更好,虽然将bool 转换为int 以与if 语句中的int 进行比较似乎真的很尴尬。
【解决方案3】:

我相信这接近于人类编写的内容(而不是反编译器)。

不知道为什么 OrderBy 是一个 int?,但不管它...

编辑

另请注意,您帖子中的条件运算符在 true 时返回 1,在 false 时返回 bool。在您的帖子中解决问题后(无法将 bool 转换为 int),您将收到 can't convert int to bool 错误。

int? _OrderBy;
public int? OrderBy
{
    get
    {
        return _OrderBy;
    }
    set
    {
        if ((_OrderBy.GetValueOrDefault() != value.GetValueOrDefault()) || (_OrderBy.HasValue != value.HasValue))
        {
            SendPropertyChanging();
            _OrderBy = value;
            SendPropertyChanged("OrderBy");
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-16
    • 2019-09-30
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    相关资源
    最近更新 更多