相关疑难解决方法(0)

使用AutoMapper自定义映射

我有两个非常简单的对象:

public class CategoryDto
{
    public string Id { get; set; }

    public string MyValueProperty { get; set; }
}

public class Category
{
    public string Id { get; set; }

    [MapTo("MyValueProperty")]
    public string Key { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

使用AutoMapper Category将a 映射到a CategoryDto时,我想要以下行为:

除了具有该MapTo属性的属性之外,应该像往常一样映射属性.在这种情况下,我必须读取Attribute的值来查找target属性.source属性的值用于在destination属性中查找要注入的值(借助字典).一个例子总是好于1000字......

例:

Dictionary<string, string> keys = 
    new Dictionary<string, string> { { "MyKey", "MyValue" } };

Category category = new Category();
category.Id = "3";
category.Key = "MyKey";

CategoryDto result = Map<Category, CategoryDto>(category);
result.Id …
Run Code Online (Sandbox Code Playgroud)

.net c# mapping automapper

13
推荐指数
1
解决办法
1万
查看次数

标签 统计

.net ×1

automapper ×1

c# ×1

mapping ×1