📄 gpstatemethodattribute.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
namespace GPCore
{
/// <summary>
/// Use this attribute when defining you own Interface to extend IProtocol if you want to allow other people to add a method as an "Action" to a menu, and the method should only be activated in in a certain state.
/// </summary>
/// <remarks>
/// This should be used for exposing methods to plugins that dont need to know anything about it, except its existence.
/// If this attribute is present in the interface a method must be also be present with the name of the interface plus "State"
/// with its parameters being the same as this methods, to check for the <see cref="State"/> of the object to know if it should be displayed now.
/// </remarks>
/// <example>
/// Here is a sample Interface and its implementation.
/// <code>
/// public interface IDoStuff
/// {
/// [GPStateMethod("Turn On","Account","On",false)]
/// void TurnOn(SqlAccount a);
/// [GPStateMethod("Turn Off","Account","On",true)]
/// void TurnOff(SqlAccount a);
/// string IDoStuffState(SqlAccount a);
/// }
/// public class DoStuff : IDoStuff
/// {
/// List<SqlAccount> AccountsOn = new List<SqlAccount>
/// public void TurnOn(SqlAccount a)
/// {
/// AccountsOn.Add(a);
/// }
/// public void TurnOff(SqlAccount a)
/// {
/// AccountsOn.Remove(a);
/// }
/// public string IDoStuffState(SqlAccount a)
/// {
/// return (AccountsOn.Contains(a) ? "On" : "Off");
/// }
/// }
/// </code>
/// </example>
[global::System.AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public sealed class GPStateMethodAttribute : Attribute , IGPMethodAttribute
{
// See the attribute guidelines at
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconusingattributeclasses.asp
/// <summary>
/// Creates a new GPMethodAttribute.
/// </summary>
/// <param name="name">The Text that will be put in the menu.</param>
/// <param name="action">What kind of feature is this. Presently Core only supports "Gib" or "Account".</param>
/// <param name="state">A string representing an on/off type state.</param>
/// <param name="onstate">True if you want to show this action when the state is equal to <paramref name="state"/>; otherwise false.</param>
/// <param name="methodname">The name of the state method to call.</param>
public GPStateMethodAttribute(String name, Type action, object state, bool onstate, string methodname)
{
this.m_action = action;
this.m_name = name;
this.m_onstate = onstate;
this.m_state = state;
this.m_method_name = methodname;
}
private string m_name;
/// <summary>
/// The text that will be put in the menu.
/// </summary>
public string Name
{
get { return m_name; }
}
private Type m_action;
/// <summary>
/// The type of the element it modifies.
/// </summary>
public Type Action
{
get { return m_action; }
}
private object m_state;
/// <summary>
/// A string representing an on/off type state.
/// </summary>
public object State
{
get { return m_state; }
}
private bool m_onstate;
/// <summary>
/// True if you want to show this action when the state is equal to <see cref="State"/>; otherwise false.
/// </summary>
public bool OnState
{
get { return m_onstate; }
}
private string m_method_name;
public string MethodName
{
get { return m_method_name; }
}
#region IGPMethodAttribute Members
public bool Show(object target, GPCore.Protocols.IProtocol caller)
{
if (!m_action.IsAssignableFrom(target.GetType())) return false;
else
{
object o = caller.GetType().GetMethod(m_method_name).Invoke(caller, new object[] { target });
return (m_state.Equals(o) == m_onstate);
}
}
public void PerformAction(object target, GPCore.Protocols.IProtocol caller, System.Reflection.MethodInfo method)
{
try
{
if (m_action == typeof(User))
method.Invoke(caller,null);
else
method.Invoke(caller, new object[] { target });
}
catch (Exception e)
{
Logger.Log(e);
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -