简体   繁体   中英

An unhandled exception of type 'System.TypeInitializationException' occurred in ConsoleApplication.exe - Automapper

After taking a look at Automapper attributes I have tried to answer this question so I have made a quick Console application to reproduce the behavior. I have added (copy-pasted) the classes in the first example from the GitHub documentation:

[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; }
}

And added the same statements in the main:

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

but at the first line of code in main, when calling MapTypes method, an exception of type TypeInitializationException is thrown:

An unhandled exception of type 'System.TypeInitializationException' occurred in ConsoleApplication.exe

Additional information: The type initializer for 'AutoMapper.Attributes.Extensions' threw an exception.

Why is this exception thrown, since I have followed all the instructions in the documentation?

The inner exception is:

{"Sequence contains no matching element"}

NOTE:

I am using AutoMapper version 5.1.1 and AutoMapper.Attributes version 1.0.20.

Looks like you are using Automapper 5, but the Automapper.Attributes seems only to work with version 4. I tried it with version 4 and it worked as expected.

The problem seems to be that a method signature changed, which is looked up via reflection in Attributes.Extensions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM