标签: automapper-10

如何使用 AutoMapper 将 byte[] 转换为字符串?

当尝试将具有 byte[] 类型属性的对象转换为具有字符串类型匹配属性的对象时,我在 AutoMapper 中收到以下错误:

System.InvalidOperationException:缺少从 System.Byte 到 System.Char 的映射。使用 CreateMap<Byte, Char> 创建。

我尝试使用自定义类型转换器,但它们似乎不起作用(无论有或没有它们都会出现相同的错误)。我能够映射特定的属性,但我正在尝试创建可以应用于整个项目的东西(我的大多数实体都包含旨在用于乐观锁定的 RowVersion)。

我的代码看起来像这样......

public class AutoMapperProfile : AutoMapper.Profile
{
    public AutoMapperProfile()
    {
        CreateMap<byte[], string>().ConvertUsing<ByteArrayToStringTypeConverter>();
        CreateMap<string, byte[]>().ConvertUsing<StringToByteArrayTypeConverter>();
        CreateMap<MyFirstClass, MySecondClass>();
    }
}
public class MyFirstClass
{
    public string Name { get; set; }
    public byte[] RowVersion { get; set; }
}

public class MySecondClass
{
    public string Name { get; set; }
    public string RowVersion { get; set; }
}

public class ByteArrayToStringTypeConverter : ITypeConverter<byte[], string>
{
    public string …
Run Code Online (Sandbox Code Playgroud)

c# automapper asp.net-core automapper-10

4
推荐指数
1
解决办法
1746
查看次数

标签 统计

asp.net-core ×1

automapper ×1

automapper-10 ×1

c# ×1