I'm trying to figure out how to prevent AutoMapper from ever putting a null into a constructor argument marked with a [NotNull] attribute.
For example, given the following:
public class MyNullClass
{
public string MyProp { get; set; }
}
public class MyNotNullClass
{
public MyNotNullClass([NotNull] string myProp) =>
System.Console.WriteLine(myProp == "Foo" ? "Foo" : "Bar");
}
I would like this code:
var myClass = new MyNullClass();
var iShouldCrash = _mapper.Map<MyNotNullClass>(myClass);
To throw a NullArgumentException with a message of "AutoMapper tried to map null to a member marked [NotNull].".
...But I'm not sure how. How can I hook something into AutoMapper to check the actual mapping being done?
I've tried BeforeMap, and then using reflection, but that seems to run after the mapping has already been performed and it's for the entire object, so I have no idea how to tell which constructor was actually called with which values.
Aucun commentaire:
Enregistrer un commentaire