2016-10-26 57 views
0

看過Automapper attributes之後,我試圖回答this question,因此我已經制作了一個快速的Console應用程序來重現此行爲。我已經加入(複製粘貼)的類中從GitHub的文檔的第一個例子:在ConsoleApplication.exe中發生未處理的類型爲「System.TypeInitializationException」的異常 - Automapper

[MapsTo(typeof(Customer))] 
public class Person 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Notes { get; set; } 
} 

public class Customer 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string MyCustomerNotes { get; set; } 
} 

並補充在主相同的語句:

typeof(Program).Assembly.MapTypes(); //this throws exception 
var person = new Person { FirstName = "John", LastName = "Lackey" }; 
var customer = AutoMapper.Mapper.Map<Customer>(person); 

但在代碼中的第一行主,調用MapTypes方法,類型的異常TypeInitializationException被拋出時:

類型的未處理的異常「System.TypeInitializ ationException'發生在ConsoleApplication.exe中

附加信息:「AutoMapper.Attributes.Extensions」的類型初始值設定項引發異常。

爲什麼拋出此異常,因爲我遵循了文檔中的所有說明?

內部異常是:

{ 「序列不包含匹配元件」}

注:

我使用AutoMapper版本5.1.1和AutoMapper.Attributes版本1.0 0.20。

+1

你能在TypeInitializationException的內部異常的報告? – Georg

+0

@Georg看起來像我有同樣的問題,但我仍然不明白爲什麼這不起作用 – meJustAndrew

回答

1

看起來你使用的是Automapper 5,但Automapper.Attributes似乎只適用於版本4.我嘗試了版本4,它按預期工作。

這個問題似乎是一個方法簽名改變,這是通過反射擡頭在Attributes.Extensions

相關問題