我正在尝试转换我的类的属性,但总是得到相同的错误
“用户定义的转换必须从分隔符类型转换为分隔符类型或转换为分隔符类型”
public class CharCashItemOutputBoxEntity : BaseEntity
{
public int Owner { get; set; }
public string Kind { get; set; }
public string RecId { get; set; }
public int Amount { get; set; }
public string StrChargeNo { get; set; }
public int Deleted { get; set; }
public DateTime CreateDate { get; set; }
public DateTime? DeleteDate { get; set; }
public string GaveCharName { get; set; }
public int Confirm { get; set; }
public int Period { get; set; }
public int Price { get; set; }
public string EvPtype { get; set; }
public string Comment { get; set; }
public static implicit operator long(BaseEntity baseEntity)
{
return baseEntity.Id;
}
}有谁知道这会是什么吗?
发布于 2021-11-09 20:36:16
您需要将隐式转换移到基类中,因为源类型必须与在其中定义它的类相同。
例如,考虑下面这两个类的代码。每个都有它自己的从所包含类型隐式转换。
public class BaseEntity
{
public long Id { get; set; }
public static implicit operator long(BaseEntity baseEntity)
{
return baseEntity.Id;
}
}
public class CharCashItemOutputBoxEntity : BaseEntity
{
public int Owner { get; set; }
public string Kind { get; set; }
public string RecId { get; set; }
public int Amount { get; set; }
public string StrChargeNo { get; set; }
public int Deleted { get; set; }
public DateTime CreateDate { get; set; }
public DateTime? DeleteDate { get; set; }
public string GaveCharName { get; set; }
public int Confirm { get; set; }
public int Period { get; set; }
public int Price { get; set; }
public string EvPtype { get; set; }
public string Comment { get; set; }
public static implicit operator string(CharCashItemOutputBoxEntity entity)
{
return entity.RecId;
}
}使用示例代码
var entity = new CharCashItemOutputBoxEntity();
long id = entity;
string recId = entity;https://stackoverflow.com/questions/69904096
复制相似问题