c# - 隐式转换
问题描述
我正在尝试转换我班级的一个属性,但我总是遇到同样的错误
“用户定义的转换必须从或转换为分隔符类型”
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;
}
}
有谁知道它可能是什么?
解决方案
您需要将隐式转换移动到基类中,因为源类型必须与定义它的类相同。
例如,考虑以下具有两个类的代码。每个都有自己从包含类型的隐式转换。
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;
推荐阅读
- django-filter - 如何过滤石墨烯-django中为空的石墨烯标量场?
- c# - 运行日历时找不到表 0
- javascript - 如何在javascript中将背景颜色从白色慢慢淡化为红色
- android - 使用开放天气 API 时如何显示天气图标?
- gnuradio - 使用 gnuradio-companion 支持编译 gnuradio-3.8.0.0
- c# - c#线程停止而不抛出错误
- c# - 如何发布我在复选框中选中或取消选中的选项
- jmeter - 使用 Jmeter 执行分布式负载测试时的端口
- c# - Rijndael not decrypting correctly
- amazon-web-services - 如何使用本地文件夹而不是 S3 存储桶作为集合?