首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >隐式转换

隐式转换
EN

Stack Overflow用户
提问于 2021-11-09 19:39:33
回答 1查看 54关注 0票数 0

我正在尝试转换我的类的属性,但总是得到相同的错误

“用户定义的转换必须从分隔符类型转换为分隔符类型或转换为分隔符类型”

代码语言:javascript
复制
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;
    }
}

有谁知道这会是什么吗?

EN

回答 1

Stack Overflow用户

发布于 2021-11-09 20:36:16

您需要将隐式转换移到基类中,因为源类型必须与在其中定义它的类相同。

例如,考虑下面这两个类的代码。每个都有它自己的从所包含类型隐式转换

代码语言:javascript
复制
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;
    }
}

使用示例代码

代码语言:javascript
复制
var entity = new CharCashItemOutputBoxEntity();

long id = entity;
string recId = entity;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69904096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档