PluginAttribute.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. // .NET Compact Framework 1.0 has no support for reading assembly attributes
  20. #if !NETCF
  21. using System;
  22. using System.Globalization;
  23. using System.Reflection;
  24. using log4net.Core;
  25. using log4net.Util;
  26. using log4net.Plugin;
  27. namespace log4net.Config
  28. {
  29. /// <summary>
  30. /// Assembly level attribute that specifies a plugin to attach to
  31. /// the repository.
  32. /// </summary>
  33. /// <remarks>
  34. /// <para>
  35. /// Specifies the type of a plugin to create and attach to the
  36. /// assembly's repository. The plugin type must implement the
  37. /// <see cref="IPlugin"/> interface.
  38. /// </para>
  39. /// </remarks>
  40. /// <author>Nicko Cadell</author>
  41. /// <author>Gert Driesen</author>
  42. [AttributeUsage(AttributeTargets.Assembly,AllowMultiple=true)]
  43. [Serializable]
  44. public sealed class PluginAttribute : Attribute, IPluginFactory
  45. {
  46. #region Public Instance Constructors
  47. #if !NETSTANDARD1_3 // Excluded because GetCallingAssembly() is not available in CoreFX (https://github.com/dotnet/corefx/issues/2221).
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="PluginAttribute" /> class
  50. /// with the specified type.
  51. /// </summary>
  52. /// <param name="typeName">The type name of plugin to create.</param>
  53. /// <remarks>
  54. /// <para>
  55. /// Create the attribute with the plugin type specified.
  56. /// </para>
  57. /// <para>
  58. /// Where possible use the constructor that takes a <see cref="System.Type"/>.
  59. /// </para>
  60. /// </remarks>
  61. public PluginAttribute(string typeName)
  62. {
  63. m_typeName = typeName;
  64. }
  65. #endif
  66. /// <summary>
  67. /// Initializes a new instance of the <see cref="PluginAttribute" /> class
  68. /// with the specified type.
  69. /// </summary>
  70. /// <param name="type">The type of plugin to create.</param>
  71. /// <remarks>
  72. /// <para>
  73. /// Create the attribute with the plugin type specified.
  74. /// </para>
  75. /// </remarks>
  76. public PluginAttribute(Type type)
  77. {
  78. m_type = type;
  79. }
  80. #endregion Public Instance Constructors
  81. #region Public Instance Properties
  82. /// <summary>
  83. /// Gets or sets the type for the plugin.
  84. /// </summary>
  85. /// <value>
  86. /// The type for the plugin.
  87. /// </value>
  88. /// <remarks>
  89. /// <para>
  90. /// The type for the plugin.
  91. /// </para>
  92. /// </remarks>
  93. public Type Type
  94. {
  95. get { return m_type; }
  96. set { m_type = value ; }
  97. }
  98. /// <summary>
  99. /// Gets or sets the type name for the plugin.
  100. /// </summary>
  101. /// <value>
  102. /// The type name for the plugin.
  103. /// </value>
  104. /// <remarks>
  105. /// <para>
  106. /// The type name for the plugin.
  107. /// </para>
  108. /// <para>
  109. /// Where possible use the <see cref="Type"/> property instead.
  110. /// </para>
  111. /// </remarks>
  112. public string TypeName
  113. {
  114. get { return m_typeName; }
  115. set { m_typeName = value ; }
  116. }
  117. #endregion Public Instance Properties
  118. #region Implementation of IPluginFactory
  119. /// <summary>
  120. /// Creates the plugin object defined by this attribute.
  121. /// </summary>
  122. /// <remarks>
  123. /// <para>
  124. /// Creates the instance of the <see cref="IPlugin"/> object as
  125. /// specified by this attribute.
  126. /// </para>
  127. /// </remarks>
  128. /// <returns>The plugin object.</returns>
  129. public IPlugin CreatePlugin()
  130. {
  131. Type pluginType = m_type;
  132. #if !NETSTANDARD1_3
  133. if (m_type == null)
  134. {
  135. // Get the plugin object type from the string type name
  136. pluginType = SystemInfo.GetTypeFromString(m_typeName, true, true);
  137. }
  138. #endif
  139. // Check that the type is a plugin
  140. if (!(typeof(IPlugin).IsAssignableFrom(pluginType)))
  141. {
  142. throw new LogException("Plugin type [" + pluginType.FullName + "] does not implement the log4net.IPlugin interface");
  143. }
  144. // Create an instance of the plugin using the default constructor
  145. IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);
  146. return plugin;
  147. }
  148. #endregion Implementation of IPluginFactory
  149. #region Override implementation of Object
  150. /// <summary>
  151. /// Returns a representation of the properties of this object.
  152. /// </summary>
  153. /// <remarks>
  154. /// <para>
  155. /// Overrides base class <see cref="M:Object.ToString()" /> method to
  156. /// return a representation of the properties of this object.
  157. /// </para>
  158. /// </remarks>
  159. /// <returns>A representation of the properties of this object</returns>
  160. override public string ToString()
  161. {
  162. if (m_type != null)
  163. {
  164. return "PluginAttribute[Type=" + m_type.FullName + "]";
  165. }
  166. return "PluginAttribute[Type=" + m_typeName + "]";
  167. }
  168. #endregion Override implementation of Object
  169. #region Private Instance Fields
  170. private string m_typeName = null;
  171. private Type m_type = null;
  172. #endregion Private Instance Fields
  173. }
  174. }
  175. #endif // !NETCF