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

Bri*_*ian 4 c# automapper asp.net-core automapper-10

当尝试将具有 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 Convert(byte[] source, string destination, ResolutionContext context)
    {
        return System.Convert.ToBase64String(source);
    }
}

public class StringToByteArrayTypeConverter : ITypeConverter<string, byte[]>
{
    public byte[] Convert(string source, byte[] destination, ResolutionContext context)
    {
        return System.Convert.FromBase64String(source);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个 .Net 5、ASP.Net Core、Web API 项目。

abd*_*sco 5

TypeConverter我这边工作得很好:

鉴于这些课程:

class DatabaseRecord
{
    public byte[] RowVersion { get; set; }
}

class Dto
{
    public string RowVersion { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

和这个映射配置

public class Mappings : Profile
{
    public static IMapper Mapper = new MapperConfiguration(c => c.AddProfile(new Mappings())).CreateMapper();

    public Mappings()
    {
        CreateMap<string, byte[]>().ConvertUsing<Base64Converter>();
        CreateMap<byte[], string>().ConvertUsing<Base64Converter>();
        CreateMap<DatabaseRecord, Dto>().ReverseMap();
    }

    private class Base64Converter : ITypeConverter<string, byte[]>, ITypeConverter<byte[], string>
    {
        public byte[] Convert(string source, byte[] destination, ResolutionContext context) 
            => System.Convert.FromBase64String(source);

        public string Convert(byte[] source, string destination, ResolutionContext context) 
            => System.Convert.ToBase64String(source);
    }
}
Run Code Online (Sandbox Code Playgroud)

@Lucian Bargaoanu指出这可以通过内联转换器来缩短:

CreateMap<string, byte[]>().ConvertUsing(s => System.Convert.FromBase64String(s));
CreateMap<byte[], string>().ConvertUsing(bytes => System.Convert.ToBase64String(bytes));
CreateMap<DatabaseRecord, Dto>().ReverseMap();
Run Code Online (Sandbox Code Playgroud)

我可以string在 (base64)byte[]和 (base64) 之间进行转换,没有任何问题:

var result = Mappings.Mapper.Map<Dto>(new DatabaseRecord
{
    RowVersion = new byte[]{104,101,108,108,111} // "hello"
});
Console.WriteLine(result.RowVersion); // outputs "aGVsbG8="

var record = Mappings.Mapper.Map<DatabaseRecord>(new Dto
{
    RowVersion = "aGVsbG8=" // "hello" in base64
});
// record.RowVersion == 104,101,108,108,111
Run Code Online (Sandbox Code Playgroud)

  • 您可以在此处使用内联类型转换器。更容易阅读。 (2认同)