相关疑难解决方法(0)

配置AutoMapper以映射到具体类型,但允许在我的类的定义中使用接口

我有一些类似于下面的代码.基本上它表示从Web服务获取数据并将其转换为客户端对象.

void Main()
{
    Mapper.CreateMap<SomethingFromWebService, Something>();    
    Mapper.CreateMap<HasSomethingFromWebService, HasSomething>(); 
    // Service side
    var hasSomethingFromWeb = new HasSomethingFromWebService();
    hasSomethingFromWeb.Something = new SomethingFromWebService
            { Name = "Whilly B. Goode" };
    // Client Side                
    HasSomething hasSomething=Mapper.Map<HasSomething>(hasSomethingFromWeb);  
}    
// Client side objects
public interface ISomething
{
    string Name {get; set;}
}    
public class Something : ISomething
{
    public string Name {get; set;}
}    
public class HasSomething
{
    public ISomething Something {get; set;}
}    
// Server side objects
public class SomethingFromWebService
{
    public string Name {get; …
Run Code Online (Sandbox Code Playgroud)

.net c# automapper

20
推荐指数
1
解决办法
8766
查看次数

自动映射到接口:"TypeLoadException"异常

我正在研究一个使用Inversion of Control的MVC应用程序,因此广泛使用了接口类型,具体实现由依赖解析器根据需要注入.实体接口继承自描述实体的一些基本管理功能的基本接口.ViewModels也被广泛使用.

该应用程序使用Automapper,我已经创建了从视图模型到各种实体接口的映射.映射配置正确验证.但是,当我调用Automapper执行映射时,代码失败了TypeLoadException.

我相信Automapper能够映射接口(见吉米博加德).

似乎Automapper代理生成器可能已省略将MyMethod()添加到代理,这会在Reflection尝试创建类型时导致异常.

如果不是这样,我该如何让这张地图发挥作用?我错过了一些明显的事吗?

这是一个简化的控制台应用程序,用于演示场景,并在运行时重现错误:

public interface IEntity
{
    string Foo { get; set; }
    string Bar { get; set; }
    string MyMethod();
}

public class MyEntity : IEntity
{
    public string Foo { get; set; }
    public string Bar { get; set; }
    public string MyMethod()
    {
        throw new NotImplementedException();
    }
}

public class MyViewModel
{
    public string Foo { get; set; }
    public string Bar { get; set; }
}

class …
Run Code Online (Sandbox Code Playgroud)

interface automapper

11
推荐指数
1
解决办法
4141
查看次数

标签 统计

automapper ×2

.net ×1

c# ×1

interface ×1