【问题标题】:Writing a generic Resolver in AutoMapper?在 AutoMapper 中编写通用解析器?
【发布时间】:2013-11-15 09:21:29
【问题描述】:

我想编写一个通用 AutoMapper 的解析器来更改模型的文件路径。
如果没有通用解析器,我编写了以下解析器:
例如:

public class UserPhotoPathResolver : ValueResolver<User, string>
{
    protected override string ResolveCore(User source)
    {
        var url = new UrlHelper(HttpContext.Current.Request.RequestContext);
        return url.Content(string.IsNullOrWhiteSpace(source.PhotoPath) 
            ? StaticVariables.DefaultUserImagePath 
            : source.PhotoPath);
    }
}

现在,我编写了以下解析器:

public class FilePathResolver<T, TProperty> : ValueResolver<T, string> where T : class
{
    private readonly Expression<Func<T, TProperty>> _propertyExpression;

    public FilePathResolver(Expression<Func<T, TProperty>> propertyExpression)
    {
        _propertyExpression = propertyExpression;
    }

    protected override string ResolveCore(T source)
    {
        Type typeOfEntity = typeof(T);

        MemberExpression member = _propertyExpression.Body as MemberExpression;
        if (member == null)
            throw new ArgumentException(string.Format("Expression '{0}' refers to a method, not a property.", _propertyExpression));

        PropertyInfo propInfo = member.Member as PropertyInfo;
        if (propInfo == null)
            throw new ArgumentException(string.Format("Expression '{0}' refers to a field, not a property.", _propertyExpression));

        if (typeOfEntity != propInfo.ReflectedType && !typeOfEntity.IsSubclassOf(propInfo.ReflectedType))
            throw new ArgumentException(string.Format("Expresion '{0}' refers to a property that is not from type {1}.", _propertyExpression, typeOfEntity));

        string filePath = Convert.ToString(ModelHelpers.GetStringPropertyValue(source, propInfo.Name));
        return string.IsNullOrWhiteSpace(filePath)
            ? string.Empty
            : UrlHelpers.GetUrlHelperInstance().Content(filePath);
    }
}

public static object GetStringPropertyValue(object src, string propertyName)
{
    return src.GetType().GetProperty(propertyName).GetValue(src, null);
}

public static TProperty GetValue<T, TProperty>(T obj, Expression<Func<T, TProperty>> expression) where T : class
{
    if (obj == null) return default(TProperty);
    Func<T, TProperty> func = expression.Compile();
    return func(obj);
}

但是FilePathResolver返回这个字符串MyApp.Classes.Helpers.FilePathResolver%602[MyApp.DAL.ModelName,System.String]

我使用这个解析器如下:

Mapper.CreateMap<EntityClass, EntityClassModel>()
    .ForMember(m => m.ResolvedLogoPath, opt => opt.ResolveUsing(m => new FilePathResolver<EntityClass, string>(p => p.LogoPath)));

我该怎么做?

【问题讨论】:

  • 请发布ModelHelpers.GetStringPropertyValue 内容。看起来问题出在其实现的某个地方。
  • @k0stya 写在Middle Code Box的

标签: c# generics mapping automapper


【解决方案1】:

问题是使用了错误的ResolveUsing方法重载。

但你需要以下一个。

您可以通过以下方式更改映射配置来修复它。

Mapper.CreateMap<EntityClass, EntityClassModel>()
    .ForMember(m => m.ResolvedLogoPath, 
        opt => opt.ResolveUsing<FilePathResolver<EntityClass, string>>()
        .ConstructedBy(() => new FilePathResolver<EntityClass, string>(p => p.LogoPath)));

甚至像这样:

Mapper.CreateMap<EntityClass, EntityClassModel>()
  .ForMember(m => m.ResolvedLogoPath, 
    opt => opt.ResolveUsing(new FilePathResolver<EntityClass, string>(p => p.LogoPath)));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    • 2018-08-07
    • 2014-03-27
    相关资源
    最近更新 更多