📄 gpargumentmethodattribute.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Reflection.Emit;
using System.Drawing;
using GPCore.Exceptions;
using System.Threading;
using System.IO;
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 GPArgumentMethodAttribute : 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>
/// <param name="args">A List of Arguments in the format (name, Type)</param>
/// <example>
/// A sample Usage is
/// <code>
/// public interface IDOStuff
/// {
/// [GPArgumentMethod("Do Something",typeof(User),"Name",typeof(string),"ID", typeof(int),"When",typeof(DateTime))]
/// void DoSomething(String name, int id, DateTime time);
/// }
/// </code>
/// </example>
public GPArgumentMethodAttribute(String name, Type action, params object[] args)
{
this.m_action = action;
this.m_name = name;
this.m_args = args;
}
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_args;
public Dictionary<string, Type> Arguments
{
get
{
Dictionary<string, Type> dict = new Dictionary<string, Type>();
for (int i = 0; i < m_args.Length; i += 2)
{
dict.Add((m_args[i] as string), m_args[i+1] as Type);
}
return dict;
}
}
static int forms = 8;
public static Form CreateForm(Dictionary<string, Type> args)
{
AppDomain myDomain = Thread.GetDomain();
AssemblyName myAsmName = new AssemblyName();
myAsmName.Name = "GPCoreDynamicForms";
AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(
myAsmName,
AssemblyBuilderAccess.RunAndSave);
ModuleBuilder IntVectorModule = myAsmBuilder.DefineDynamicModule("GPCoreDynamicForm",true);
TypeBuilder tb = IntVectorModule.DefineType("form" + forms,
TypeAttributes.Public,typeof(Form));
tb.DefineDefaultConstructor(MethodAttributes.Public);
foreach (KeyValuePair<string, Type> pair in args)
{
FieldBuilder fb = tb.DefineField(pair.Key.Replace(' ', '_'), pair.Value, FieldAttributes.Public);
}
Type t = tb.CreateType();
Form f = t.GetConstructor(Type.EmptyTypes).Invoke(null) as Form;
f.Name = "Gibphone";
TableLayoutPanel table = new TableLayoutPanel();
int i = 0;
table.ColumnCount = 2;
table.RowCount = args.Count + 1;
foreach (KeyValuePair<string, Type> pair in args)
{
System.Windows.Forms.Label lbl = new System.Windows.Forms.Label();
lbl.Text = pair.Key;
lbl.Name = "label" + i;
table.Controls.Add(lbl, 0, i);
if (pair.Value == typeof(string))
{
TextBox box = new TextBox();
box.Name = "txt" + pair.Key.Replace(' ', '_');
box.Text = "";
f.GetType().GetField(box.Name.Substring(3)).SetValue(f, "");
box.TextChanged += delegate(object o, EventArgs e)
{
TextBox txt = o as TextBox;
FieldInfo fi = f.GetType().GetField(txt.Name.Substring(3));
fi.SetValue(f, txt.Text);
};
table.Controls.Add(box, 1, i);
}
else if (pair.Value == typeof(int))
{
TextBox box = new TextBox();
box.Name = "txt" + pair.Key.Replace(' ', '_');
f.GetType().GetField(box.Name.Substring(3)).SetValue(f, 0);
box.TextChanged += delegate(object o, EventArgs e)
{
TextBox txt = o as TextBox;
int num;
try
{
num = Convert.ToInt32(txt.Text);
}
catch (FormatException)
{
num = 0;
}
FieldInfo fi = f.GetType().GetField(txt.Name.Substring(3));
fi.SetValue(f, num);
};
table.Controls.Add(box, 1, i);
}
else if (pair.Value == typeof(double))
{
TextBox box = new TextBox();
box.Name = "txt" + pair.Key.Replace(' ', '_');
f.GetType().GetField(box.Name.Substring(3)).SetValue(f, 0);
box.TextChanged += delegate(object o, EventArgs e)
{
TextBox txt = o as TextBox;
double num;
try
{
num = Convert.ToDouble(txt.Text);
}
catch (FormatException)
{
num = 0.0;
}
FieldInfo fi = f.GetType().GetField(txt.Name.Substring(3));
fi.SetValue(f, num);
};
table.Controls.Add(box, 1, i);
}
else if (pair.Value == typeof(FileInfo))
{
Panel pan = new FlowLayoutPanel();
TextBox box = new TextBox();
box.Name = "txt" + pair.Key.Replace(' ', '_');
pan.Controls.Add(box);
Button button = new Button();
button.Name = "btn" + pair.Key.Replace(' ', '_');
button.Text = "...";
Graphics g = button.CreateGraphics();
SizeF size = g.MeasureString("...", button.Font);
button.Size = new Size((int)size.Width + 5, (int)size.Height + 5);
button.Tag = box;
pan.Controls.Add(button);
button.Click += delegate(object o, EventArgs e)
{
Button btn = o as Button;
OpenFileDialog diag = new OpenFileDialog();
if (diag.ShowDialog() == DialogResult.OK)
{
FieldInfo fi = f.GetType().GetField(btn.Name.Substring(3));
TextBox txt = btn.Tag as TextBox;
txt.Text = diag.FileName;
fi.SetValue(f, new FileInfo(diag.FileName));
}
};
table.Controls.Add(button, 1, i);
}
i++;
}
Button b = new Button();
b.Text = "Ok";
b.Click += delegate(object o, EventArgs e) { f.DialogResult = DialogResult.OK; f.Close(); };
table.Dock = DockStyle.Fill;
table.Controls.Add(b, 0, i);
b = new Button();
b.Text = "Cancel";
b.Click += delegate(object o, EventArgs e) { f.DialogResult = DialogResult.Cancel; f.Close(); };
table.Controls.Add(b, 1, i);
f.Controls.Add(table);
f.Size = table.PreferredSize + new Size(10,35);//new System.Drawing.Size((int)table.ColumnStyles[0].Width * 2 +25, (int)table.RowStyles[0].Height * table.RowCount + 25);
return f;
}
#region IGPMethodAttribute Members
public bool Show(object target, GPCore.Protocols.IProtocol caller)
{
return (m_action.IsAssignableFrom(target.GetType()));
}
public void PerformAction(object target, GPCore.Protocols.IProtocol caller, MethodInfo method)
{
Form f = CreateForm(Arguments);
f.Text = Name;
f.Tag = method;
TryAgain:
if (f.ShowDialog() == DialogResult.OK)
{
List<object> args = new List<object>();
if (m_action != typeof(User))
args.Add(target);
foreach (string param in Arguments.Keys)
{
args.Add(f.GetType().GetField(param.Replace(' ', '_')).GetValue(f));
}
try
{
method.Invoke(caller, args.ToArray());
}
catch (TryAgainException e)
{
MessageBox.Show(e.Message);
goto TryAgain;
}
catch (Exception e)
{
Logger.Log(e);
}
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -