#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
// .NET Compact Framework 1.0 has no support for reading assembly attributes
#if !NETCF
using System;
using log4net.Core;
#if !NETSTANDARD1_3
using log4net.Util;
#endif
using log4net.Plugin;
namespace log4net.Config
{
///
/// Assembly level attribute that specifies a plugin to attach to
/// the repository.
///
///
///
/// Specifies the type of a plugin to create and attach to the
/// assembly's repository. The plugin type must implement the
/// interface.
///
///
/// Nicko Cadell
/// Gert Driesen
[AttributeUsage(AttributeTargets.Assembly,AllowMultiple=true)]
[Serializable]
public sealed class PluginAttribute : Attribute, IPluginFactory
{
#region Public Instance Constructors
#if !NETSTANDARD1_3 // Excluded because GetCallingAssembly() is not available in CoreFX (https://github.com/dotnet/corefx/issues/2221).
///
/// Initializes a new instance of the class
/// with the specified type.
///
/// The type name of plugin to create.
///
///
/// Create the attribute with the plugin type specified.
///
///
/// Where possible use the constructor that takes a .
///
///
public PluginAttribute(string typeName)
{
m_typeName = typeName;
}
#endif
///
/// Initializes a new instance of the class
/// with the specified type.
///
/// The type of plugin to create.
///
///
/// Create the attribute with the plugin type specified.
///
///
public PluginAttribute(Type type)
{
m_type = type;
}
#endregion Public Instance Constructors
#region Public Instance Properties
///
/// Gets or sets the type for the plugin.
///
///
/// The type for the plugin.
///
///
///
/// The type for the plugin.
///
///
public Type Type
{
get { return m_type; }
set { m_type = value ; }
}
///
/// Gets or sets the type name for the plugin.
///
///
/// The type name for the plugin.
///
///
///
/// The type name for the plugin.
///
///
/// Where possible use the property instead.
///
///
public string TypeName
{
get { return m_typeName; }
set { m_typeName = value ; }
}
#endregion Public Instance Properties
#region Implementation of IPluginFactory
///
/// Creates the plugin object defined by this attribute.
///
///
///
/// Creates the instance of the object as
/// specified by this attribute.
///
///
/// The plugin object.
public IPlugin CreatePlugin()
{
Type pluginType = m_type;
#if !NETSTANDARD1_3
if (m_type == null)
{
// Get the plugin object type from the string type name
pluginType = SystemInfo.GetTypeFromString(m_typeName, true, true);
}
#endif
// Check that the type is a plugin
if (!(typeof(IPlugin).IsAssignableFrom(pluginType)))
{
throw new LogException("Plugin type [" + pluginType.FullName + "] does not implement the log4net.IPlugin interface");
}
// Create an instance of the plugin using the default constructor
IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);
return plugin;
}
#endregion Implementation of IPluginFactory
#region Override implementation of Object
///
/// Returns a representation of the properties of this object.
///
///
///
/// Overrides base class method to
/// return a representation of the properties of this object.
///
///
/// A representation of the properties of this object
public override string ToString()
{
if (m_type != null)
{
return "PluginAttribute[Type=" + m_type.FullName + "]";
}
return "PluginAttribute[Type=" + m_typeName + "]";
}
#endregion Override implementation of Object
#region Private Instance Fields
private string m_typeName = null;
private Type m_type = null;
#endregion Private Instance Fields
}
}
#endif // !NETCF