/// <summary> /// 转换。 /// </summary> /// <param name="instance">需要转换的实例。</param> /// <param name="conversionType">转换的类型。</param> /// <returns>转换之后的类型,如果无法转换则返回null。</returns> /// <exception cref="ArgumentNullException">instance</exception> /// <exception cref="ArgumentNullException">conversionType</exception> /// <exception cref="ArgumentNullException">instance</exception> /// <exception cref="ArgumentNullException">conversionType</exception> public object Convert(object instance, Type conversionType) { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } if (conversionType == null) { throw new ArgumentNullException(nameof(conversionType)); } if (conversionType.GetTypeInfo().IsInstanceOfType(instance)) { return(instance); } if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"准备将 {instance.GetType()} 转换为:{conversionType}。"); } object result = null; foreach (var converter in _converters) { result = converter(instance, conversionType); if (result != null) { break; } } if (result != null) { return(result); } var exception = new CPlatformException($"无法将实例:{instance}转换为{conversionType}。"); if (_logger.IsEnabled(LogLevel.Error)) { _logger.LogError(exception, $"将 {instance.GetType()} 转换成 {conversionType} 时发生了错误。"); } throw exception; }
public static Exception GetExceptionByStatusCode(this RemoteInvokeResultMessage message) { Exception exception = null; switch (message.StatusCode) { case StatusCode.BusinessError: exception = new BusinessException(message.ExceptionMessage); break; case StatusCode.CommunicationError: exception = new CommunicationException(message.ExceptionMessage); break; case StatusCode.RequestError: case StatusCode.CPlatformError: case StatusCode.UnKnownError: exception = new CPlatformException(message.ExceptionMessage, message.StatusCode); break; case StatusCode.DataAccessError: exception = new DataAccessException(message.ExceptionMessage); break; case StatusCode.UnAuthentication: exception = new UnAuthenticationException(message.ExceptionMessage); break; case StatusCode.UnAuthorized: exception = new UnAuthorizedException(message.ExceptionMessage); break; case StatusCode.UserFriendly: exception = new UserFriendlyException(message.ExceptionMessage); break; case StatusCode.ValidateError: exception = new ValidateException(message.ExceptionMessage); break; default: exception = new CPlatformException(message.ExceptionMessage, message.StatusCode); break; } return(exception); }
private object ConvertInstaceType(object instance, Type conversionType) { object result = null; foreach (var converter in _converters) { result = converter(instance, conversionType); if (result != null) { break; } } if (result != null) { return(result); } var exception = new CPlatformException($"无法将实例:{instance}转换为{conversionType}。", StatusCode.CPlatformError); if (_logger.IsEnabled(LogLevel.Error)) { _logger.LogError(exception, $"将 {instance.GetType()} 转换成 {conversionType} 时发生了错误。"); } throw exception; }