AutoMapper 自定义解析器不能用作类型参数

问题描述 投票:0回答:1

我正在尝试为我的 AutoMapper 创建一个自定义解析器,该解析器与 AutoMapper 团队的自定义解析器的源文档几乎相同。我有一个名为

IValueResolver
NluProgressResolver

实现
public class NluProgressResolver : IValueResolver<CluTrainingJobStateModel, NluTrainingStateDto, object>
{
    public object Resolve(CluTrainingJobStateModel source, NluTrainingStateDto dest, object member, ResolutionContext context)
        => new NluTrainingProgress()
        { 
            Percentage = (source.Result.EvaluationStatus!.PercentComplete + source.Result.TrainingStatus!.PercentComplete) / 2
        };
}

其来源为

CluTrainingJobStateModel
,目的地为
NluTrainingStateDto
,目标目的地成员为
NluTrainingProgress

public class CluTrainingJobStateModel
{
    [JsonPropertyName("result")]
    public required CluTrainingJobStateResult Result { get; set; }
    [JsonPropertyName("jobId")]
    public required string JobId { get; set; }
    [JsonPropertyName("createdDateTime")]
    public string? CreatedDateTime { get; set; }
    [JsonPropertyName("lastUpdatedDateTime")]
    public string? LastUpdatedDateTime { get; set; }
    [JsonPropertyName("expirationDateTime")]
    public string? ExpirationDateTime { get; set; }
    [JsonPropertyName("status")]
    public CluTrainingJobStatus? Status { get; set; }
}
public class NluTrainingStateDto
{
    [JsonPropertyName("id")]
    public required string Id { get; set; }
    [JsonPropertyName("status")]
    public NluTrainingStatus? Status { get; set; }
    [JsonPropertyName("progress")]
    public NluTrainingProgress? Progress { get; set; }
}
public struct NluTrainingProgress
{
    [JsonPropertyName("percentage")]
    public required int Percentage { get; set; }
}

然后我尝试在

CreateMap
中使用它,如下所示:

CreateMap<CluTrainingJobStateModel, NluTrainingStateDto>()
    .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.JobId))
    .ForMember(dest => dest.Status, opt => opt.MapFrom(src => src.Status))
    .ForMember(dest => dest.Progress, opt => opt.MapFrom<NluProgressResolver>());

这会导致

NluProgressResolver
出现错误,如下所示 ->

类型“Namespace.NluProgressResolver”不能用作泛型类型或方法“IMemberConfigurationExpression.MapFrom()”中的类型参数“TValueResolver”。没有从“Namespace.NluProgressResolver”到“AutoMapper.IValueResolver”的隐式引用转换。 (我将实际名称空间缩短为“Namespace for”以使其更易于阅读)

我还想补充一点,我在解析器中将

NluTrainingProgress
替换为
object
,但它显示了完全相同的错误,而是引用了
object
,其中显示
Namespace.NluTrainingProgress?

附注我知道这可能没有遵循一些最佳实践,因此欢迎任何提示。

c# asp.net automapper
1个回答
0
投票

这可能会很讽刺,因为这也是我之前尝试过的解决方案。将

NluTrainingProgress
类的所有
NluProgressResolver
类型更改为
NluTrainingProgress?
解决了我的问题,而之前没有解决。

我想分享我自己问题的答案,以防有人遇到这个问题。


public class NluProgressResolver : IValueResolver<CluTrainingJobStateModel, NluTrainingStateDto, NluTrainingProgress?>
{
    public NluTrainingProgress? Resolve(CluTrainingJobStateModel source, NluTrainingStateDto dest, NluTrainingProgress? member, ResolutionContext context)
        => new NluTrainingProgress()
        { 
            Percentage = (source.Result.EvaluationStatus!.PercentComplete + source.Result.TrainingStatus!.PercentComplete) / 2
        };
}
© www.soinside.com 2019 - 2024. All rights reserved.