vendredi 1 avril 2016

Getting nullable object name via reflection from exception

We've lots of mapper classes and disencouraged to refactor (like checking with .HasValue). In short mapping as following shortly:

public static MyDto MyEntityToMyDto(MyEntity entity)
    {
        MyDto dto = new MyDto ();
        try
        {
            dto.DtoAge = entity.Age.Value;
            dto.DtoBirthDate = entity.Birthdate.Value;
            dto.DtoNumber = entity.Number.Value;
        }
        catch (InvalidOperationException ex)
        {
            //Throw CustomException with message including the property name which is null like "Age field is null"
        }

        return dto;
    }

Seems exception Stacktrace knows which line it occured.

Does InvalidOperationException has any information about the field which it occured to get info via reflection? Can it be possible to obtain this information?

Or can it be achieved with ExceptionResource resource? If so how?

Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire