Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将类型bool转换为int_C#_.net_Linq_Linq To Sql - Fatal编程技术网

C# 无法将类型bool转换为int

C# 无法将类型bool转换为int,c#,.net,linq,linq-to-sql,C#,.net,Linq,Linq To Sql,我丢失了源代码,不得不从DLL进行反编译,并且在LINQ to SQL DataContext类的代码中收到以下错误 错误CS0030无法将类型“bool”转换为“int” 看起来VS生成的代码出于某种奇怪的原因使用int而不是bool。见下面的星号线。我应该怎么做来修复这个铸造错误?这门课已经用了上百次了 public int? OrderBy { get { return this._OrderBy; }

我丢失了源代码,不得不从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反编译生成的。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");
            }
        }
    }

这个问题可能应该删除。在评论中提出建议。

您可能希望:

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

这应该会得到与您所得到的结果相同的结果,但可读性要高得多

您不能直接将
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)
但是,我认为您可以直接进行比较,因为使用了“lifted”操作符。从C#规范:

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

所以你可以用:

if (nullable != nullable1)
当然,这意味着您可以删除
nullable
nullable1
变量,并使用

if (this._OrderBy != value)

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

不确定OrderBy为什么是int?,但请别管它

编辑

还要注意的是,post中的条件运算符在true时返回1,在false时返回bool。在修复了帖子中的问题(无法将bool转换为int)之后,您将得到一个无法将int转换为bool的错误

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

问题是
(int)(nullable.HasValue!=nullable1.HasValue)
。这是将
bool
转换为
int
。问题是他们使用
(int)(nullable.HasValue!=nullable1.HasValue)将
bool
转换为
int
,同意,因此,我的
不能直接将int转换为bool
,也不能将
转换为Convert.ToInt32
。不,问题是将
bool
转换为
int
,而不是
int
转换为
bool
。对不起@juharr,我误读了你写的内容,并且在我的回答中对它进行了前后描述(尽管修复是正确的?). 我现在已经更新了它(希望)更清晰。是的,这看起来更好,尽管将
bool
转换为
int
进行比较,并且
int
if
语句中看起来非常尴尬。看起来您只是想要
if(this.\u OrderBy!=value)