看過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。
你能在TypeInitializationException的內部異常的報告? – Georg
@Georg看起來像我有同樣的問題,但我仍然不明白爲什麼這不起作用 – meJustAndrew