| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ViewResolutionException.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | |
| | | 9 | | namespace MyNet.UI.Locators; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Thrown when a view cannot be resolved from a view model type or instantiated. |
| | | 13 | | /// </summary> |
| | | 14 | | public sealed class ViewResolutionException : InvalidOperationException |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Initializes a new instance of the <see cref="ViewResolutionException"/> class. |
| | | 18 | | /// </summary> |
| | | 19 | | public ViewResolutionException() |
| | 0 | 20 | | : this("View resolution failed.", null, null, null) |
| | | 21 | | { |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="ViewResolutionException"/> class with a message. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="message">The error message.</param> |
| | | 28 | | public ViewResolutionException(string message) |
| | 0 | 29 | | : this(message, null, null, null) |
| | | 30 | | { |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the <see cref="ViewResolutionException"/> class with a message and inner exception |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="message">The error message.</param> |
| | | 37 | | /// <param name="innerException">The inner exception.</param> |
| | | 38 | | public ViewResolutionException(string message, Exception innerException) |
| | 0 | 39 | | : this(message, null, null, innerException) |
| | | 40 | | { |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets the view model type involved in the failure, when applicable. |
| | | 45 | | /// </summary> |
| | | 46 | | public Type? ViewModelType { get; } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets the view type involved in the failure, when applicable. |
| | | 50 | | /// </summary> |
| | | 51 | | public Type? ViewType { get; } |
| | | 52 | | |
| | | 53 | | private ViewResolutionException(string message, Type? viewModelType, Type? viewType, Exception? innerException) |
| | 6 | 54 | | : base(message, innerException) |
| | | 55 | | { |
| | | 56 | | ViewModelType = viewModelType; |
| | | 57 | | ViewType = viewType; |
| | 6 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Creates an exception when no view type mapping exists for a view model. |
| | | 62 | | /// </summary> |
| | | 63 | | public static ViewResolutionException NoMapping(Type viewModelType) => |
| | 3 | 64 | | new( |
| | 3 | 65 | | $"No view type mapping found for view model '{viewModelType.FullName}'. " + |
| | 3 | 66 | | "Register a manual mapping via ITypeResolver.Register or add a naming convention.", |
| | 3 | 67 | | viewModelType, |
| | 3 | 68 | | null, |
| | 3 | 69 | | null); |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Creates an exception when a view type cannot be instantiated. |
| | | 73 | | /// </summary> |
| | | 74 | | public static ViewResolutionException CannotInstantiateView(Type viewType, Exception? innerException = null) => |
| | 0 | 75 | | new( |
| | 0 | 76 | | $"Cannot create an instance of view '{viewType.FullName}'. " + |
| | 0 | 77 | | "Register the view in DI or provide a public parameterless constructor.", |
| | 0 | 78 | | null, |
| | 0 | 79 | | viewType, |
| | 0 | 80 | | innerException); |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Creates an exception when the resolved view instance is not assignable to the expected type. |
| | | 84 | | /// </summary> |
| | | 85 | | public static ViewResolutionException ResolvedViewTypeMismatch(Type viewModelType, Type resolvedViewType, Type expec |
| | 3 | 86 | | new( |
| | 3 | 87 | | $"View resolved for '{viewModelType.FullName}' is '{resolvedViewType.FullName}', " + |
| | 3 | 88 | | $"but '{expectedViewType.FullName}' was expected.", |
| | 3 | 89 | | viewModelType, |
| | 3 | 90 | | resolvedViewType, |
| | 3 | 91 | | null); |
| | | 92 | | } |
| | | 93 | | |