/src/LinFu.IoC/Configuration/RecursiveDependencyException.cs

http://github.com/philiplaureano/LinFu · C# · 56 lines · 33 code · 7 blank · 16 comment · 4 complexity · 6fa2af1a912b3fceca6ebe1992726ccc MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using LinFu.IoC.Interfaces;
  5. namespace LinFu.IoC.Configuration
  6. {
  7. /// <summary>
  8. /// The exception thrown when a recursive dependency is detected
  9. /// inside a <see cref="IServiceContainer" /> instance.
  10. /// </summary>
  11. [Serializable]
  12. public class RecursiveDependencyException : Exception
  13. {
  14. private readonly LinkedList<Type> _typeChain;
  15. /// <summary>
  16. /// Initializes the <see cref="RecursiveDependencyException" />
  17. /// class with the <paramref name="typeChain">chain</paramref>
  18. /// of depedencies that caused the exception.
  19. /// </summary>
  20. /// <param name="typeChain">The sequence of types that caused the dependency exception.</param>
  21. public RecursiveDependencyException(LinkedList<Type> typeChain)
  22. {
  23. _typeChain = typeChain;
  24. }
  25. /// <summary>
  26. /// Gets the value indicating the chain of types that caused the exception.
  27. /// </summary>
  28. public LinkedList<Type> TypeChain => new LinkedList<Type>(_typeChain);
  29. /// <summary>
  30. /// Gets the value indicating the error message from the <see cref="RecursiveDependencyException" />.
  31. /// </summary>
  32. public override string Message
  33. {
  34. get
  35. {
  36. var messageFormat = "Recursive Dependency Detected: {0}";
  37. var builder = new StringBuilder();
  38. var currentNode = _typeChain.First;
  39. while (currentNode != null)
  40. {
  41. builder.AppendFormat("{0}", currentNode.Value.AssemblyQualifiedName);
  42. if (currentNode.Next != null) builder.Append("--->");
  43. currentNode = currentNode.Next;
  44. }
  45. return string.Format(messageFormat, builder);
  46. }
  47. }
  48. }
  49. }