⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gpmethodattribute.cs

📁 Gibphone is CSharp Program, it can tell you how to design p2p chat.
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using GPCore.Protocols;
using System.Reflection;

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.
    /// </summary>
    /// <remarks>
    /// This should be used for exposing methods to plugins that dont need to know anything about it, except its existence.
    /// </remarks>
    [global::System.AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public sealed class GPMethodAttribute : 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">The Type of element this method modifies.</param>
        public GPMethodAttribute(String name, Type action)
        {
            this.m_action = action;
            this.m_name = name;
        }

        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; }
        }
        public bool Show(object target, IProtocol caller)
        {
            return m_action.IsAssignableFrom(target.GetType());
        }
        public void PerformAction(object target, IProtocol caller, 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);
            }
        }
    }
    public interface IGPMethodAttribute
    {
        bool Show(object target, IProtocol caller);
        void PerformAction(object target, IProtocol caller, MethodInfo method);
        string Name { get;}
    }
    public interface IGPDefaultAttribute
    {
        void PerformDefault(object target, IProtocol p);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -