string类型转为对应的枚举类型,使用Enum.Parse

来源:互联网 发布:java写web服务器 编辑:程序博客网 时间:2024/05/29 23:44
    public enum RentalState    {        Available = 0,        HandedOver,        Rented,        PaymentPending    }


 

 this.cboAvailable.Text.Equals("All") ? null : (Nullable<RentalState>)this.cboAvailable.SelectedItem,


 

因为RentalState是枚举类型,所以上面这一行出现错误:转换无效

使用Enum.Parse将string转换为对应的枚举类型

this.cboAvailable.Text.Equals("All") ? null : (Nullable<RentalState>)Enum.Parse(typeof(RentalState), this.cboAvailable.SelectedItem.ToString()),


 

原创粉丝点击