(Another snippet I originally posted to CodeKeep.)I’m realizing most of the code I kept on CodeKeep is fairly old, circa .Net 3.0.
Code:
IPlugin Interface:
public interface IPlugin
{
void Run();
}
IPluginAttribute:
[AttributeUsage(AttributeTargets.Class)]
public class PluginAttribute : Attribute
{
private readonly Guid guid;
private readonly string requiredRight;
private readonly string menu;
/// <summary>
/// Gets a GUID that identifies the plugin. This identifier should
/// remain constant between plugin versions.
/// </summary>
public Guid Guid { get { return guid; } }
/// <summary>
/// Gets the Active Directory right that users must have in order
/// to use the plugin. The plugin itself is not expected to verify
/// user rights.
/// </summary>
public string RequiredRight { get { return requiredRight; } }
/// <summary>
/// Gets a tab-delimited string that represents the desired menu
/// hierarchy into which the plugin should be placed.
/// </summary>
/// <remarks>
/// After separation into <c>n</c> strings, the first <c>n - 1</c>
/// strings represent menu sub-levels, and the last string represents
/// the menu entry that should launch the plugin. If this property
/// is null or the empty string, the plugin should not be placed in
/// any menu structure.
/// </remarks>
public string Menu { get { return menu; } }
/// <summary>
/// Initializes a new instance of the <c>PluginAttribute</c>
/// class with the specified values for the <see cref="Guid" />,
/// <see cref="RequiredRight" />, and <see cref="Menu" />properties.
/// </summary>
/// <param name="guid" />A <see cref="System.Guid" />that uniquely
/// identifies the plugin.
/// <param name="requiredRight" />A string that names a required Active
/// Directory right
/// <param name="menu" />A string representing the desired menu
/// hierarchy.
public PluginAttribute(Guid guid, string requiredRight, string menu)
{
this.guid = guid;
this.requiredRight = requiredRight;
this.menu = menu;
}
public PluginAttribute(string guidText, string requiredRight, string menu) : this(new Guid(guidText), requiredRight, menu)
{
}
/// <summary>
/// Initializes a new instance of the <c>PluginAttribute</c>
/// class with the specified values for the <see cref="Guid" />and
/// <see cref="RequiredRight" />properties. The <see cref="Menu" />
/// property is set to the empty string.
/// </summary>
/// <param name="guid" />A <see cref="System.Guid" />that uniquely
/// identifies the plugin.
/// <param name="requiredRight" />A string that names a required Active
/// Directory right
public PluginAttribute(Guid guid, string requiredRight) : this(guid, requiredRight, "")
{
}
public PluginAttribute(string guidText, string requiredRight) : this(guidText, requiredRight, "")
{
}
}
Attribute Tag in the assembly:
[Plugin("GUID","AccessRights","Topic\tSubtopic\tName")]
Lookup:
IEnumerable types = thisAssembly.GetTypes();
foreach (Type type in types)
{
if (
(typeof(IPlugin).IsAssignableFrom(type) &&
(type.GetCustomAttributes(true).Count() > 0)
)
{
var objs = .GetCustomAttributes(true);
foreach (object obj in type.GetCustomAttributes(true))
{
var pa = obj as PluginAttribute;
if (pa != null)
{
str.Append("\t" + obj.GetType() + ": \t" + obj + Environment.NewLine);
str.Append("\tGuid: " + pa.Guid + Environment.NewLine);
str.Append("\tMenu: " + pa.Menu + Environment.NewLine);
str.Append("\tRequiredRight: " + pa.RequiredRight + Environment.NewLine);
str.Append("\tTypeId: " + pa.TypeId + Environment.NewLine);
str.Append(Environment.NewLine);
}
}
}
}
}