RawLayoutConverter.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. using System;
  20. #if NETSTANDARD1_3
  21. using System.Reflection;
  22. #endif
  23. using log4net;
  24. using log4net.Core;
  25. using log4net.Util.TypeConverters;
  26. namespace log4net.Layout
  27. {
  28. /// <summary>
  29. /// Type converter for the <see cref="IRawLayout"/> interface
  30. /// </summary>
  31. /// <remarks>
  32. /// <para>
  33. /// Used to convert objects to the <see cref="IRawLayout"/> interface.
  34. /// Supports converting from the <see cref="ILayout"/> interface to
  35. /// the <see cref="IRawLayout"/> interface using the <see cref="Layout2RawLayoutAdapter"/>.
  36. /// </para>
  37. /// </remarks>
  38. /// <author>Nicko Cadell</author>
  39. /// <author>Gert Driesen</author>
  40. public class RawLayoutConverter : IConvertFrom
  41. {
  42. #region Override Implementation of IRawLayout
  43. /// <summary>
  44. /// Can the sourceType be converted to an <see cref="IRawLayout"/>
  45. /// </summary>
  46. /// <param name="sourceType">the source to be to be converted</param>
  47. /// <returns><c>true</c> if the source type can be converted to <see cref="IRawLayout"/></returns>
  48. /// <remarks>
  49. /// <para>
  50. /// Test if the <paramref name="sourceType"/> can be converted to a
  51. /// <see cref="IRawLayout"/>. Only <see cref="ILayout"/> is supported
  52. /// as the <paramref name="sourceType"/>.
  53. /// </para>
  54. /// </remarks>
  55. public bool CanConvertFrom(Type sourceType)
  56. {
  57. // Accept an ILayout object
  58. return (typeof(ILayout).IsAssignableFrom(sourceType));
  59. }
  60. /// <summary>
  61. /// Convert the value to a <see cref="IRawLayout"/> object
  62. /// </summary>
  63. /// <param name="source">the value to convert</param>
  64. /// <returns>the <see cref="IRawLayout"/> object</returns>
  65. /// <remarks>
  66. /// <para>
  67. /// Convert the <paramref name="source"/> object to a
  68. /// <see cref="IRawLayout"/> object. If the <paramref name="source"/> object
  69. /// is a <see cref="ILayout"/> then the <see cref="Layout2RawLayoutAdapter"/>
  70. /// is used to adapt between the two interfaces, otherwise an
  71. /// exception is thrown.
  72. /// </para>
  73. /// </remarks>
  74. public object ConvertFrom(object source)
  75. {
  76. ILayout layout = source as ILayout;
  77. if (layout != null)
  78. {
  79. return new Layout2RawLayoutAdapter(layout);
  80. }
  81. throw ConversionNotSupportedException.Create(typeof(IRawLayout), source);
  82. }
  83. #endregion
  84. }
  85. }