反射
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Security.Permissions;
using System.IO;
using System.Diagnostics;
namespace chartest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Object obj;
MethodInfo CloseFunc;
MethodInfo ShowMsg;
public delegate void AutoEventHandler(int i);
AutoEventHandler test = new AutoEventHandler(testFunc);
private static void testFunc(int i)
{
}
private void testFunc1(int i)
{
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
//String pluginFilePath = Path.GetDirectoryName(Application.ExecutablePath) + "\\SelfChat.dll";
//Assembly assembly = Assembly.LoadFrom(pluginFilePath);
//Type type = assembly.GetType("SelfChat.SelfChat");
//obj = System.Activator.CreateInstance(type, new object[] { this});
//CloseFunc = type.GetMethod("CloseFunc");
//ShowMsg = type.GetMethod("ShowMsg");
//UserControl us = (UserControl)obj;
//this.splitContainer1.Panel2.Controls.Add(us);
BindingFlags myBindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
String pluginFilePath = Path.GetDirectoryName(Application.ExecutablePath) + "\\SelfChat.dll";
Assembly assembly = Assembly.LoadFrom(pluginFilePath);
Type type = assembly.GetType("SelfChat.SelfChat");
obj = System.Activator.CreateInstance(type, new object[] { this });
CloseFunc = type.GetMethod("CloseFunc");
ShowMsg = type.GetMethod("ShowMsg");
UserControl us = (UserControl)obj;
this.splitContainer1.Panel2.Controls.Add(us);
EventInfo eInfo = type.GetEvent("AutoEvent");
Type handlerType = eInfo.EventHandlerType;
MethodInfo eventHandler = test.Method;
Delegate d0 = Delegate.CreateDelegate(handlerType, eventHandler);
eInfo.AddEventHandler(obj, d0);
//EventInfo eInfo = tp.GetEvent("TestEvent");
//Type handlerType = eInfo.EventHandlerType;
///*我看了MSDN但在这里的MethodInfo还没怎么理解,求人指定下*/
//MethodInfo eventHandler = ?.GetMethod("WriteEvent");
//Delegate d0 = Delegate.CreateDelegate(handlerType, eventHandler);
//eInfo.AddEventHandler(tmpobj, d0);
}
catch
{ }
}
private void button1_Click(object sender, EventArgs e)
{
string fileName = "KillProcess.exe";
string argument = Application.StartupPath + "\\KillProcess.exe";
if (!File.Exists(argument))
{
return;
}
ProcessStartInfo ps = new ProcessStartInfo(fileName, argument);
Process p = new Process();
p.StartInfo = ps;
p.Start();
//等待启动完成,否则获取进程信息可能会失败
//p.WaitForInputIdle();
}
private void button2_Click(object sender, EventArgs e)
{
string str = (string)ShowMsg.Invoke(obj, new object[]{"1", "2"});
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
CloseFunc.Invoke(obj, null);
//System.Diagnostics.Process.GetCurrentProcess().Kill();方法二直接杀死本进程
}
catch
{
}
}
public void BaseFunc()
{
MessageBox.Show("BaseFunc");
}
public void SetIco()
{
timer1.Enabled = true;
}
bool bIsTrue = true;
private void timer1_Tick(object sender, EventArgs e)
{
if (bIsTrue)
{
notifyIcon1.Icon = new Icon("123.ico");
bIsTrue = !bIsTrue;
}
else
{
notifyIcon1.Icon = new Icon("321.ico");
bIsTrue = !bIsTrue;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
timer1.Enabled = false;
notifyIcon1.Icon = new Icon("123.ico");
}
}
}
-------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Reflection;
using System.Security.Permissions;
using System.IO;
using System.Diagnostics;
namespace SelfChat
{
public partial class SelfChat : UserControl
{
Thread thread;
//UdpClient udpClient;
Form BaseForm;
public SelfChat(Form form)
{
InitializeComponent();
BaseForm = form;
thread = new Thread(new ThreadStart(ThreadFunc));
thread.Start();
UdpClient udpClient = new UdpClient(9527);
}
void ThreadFunc()
{
while (true)
{
Thread.Sleep(1);
}
}
public void CloseFunc()
{
//udpClient.Close();
thread.Abort();
thread = null;
}
public string ShowMsg(string str1, string str2)
{
string strResult = str1 + str2;
MessageBox.Show(strResult);
return strResult;
}
private void button1_Click(object sender, EventArgs e)
{
MethodInfo method = BaseForm.GetType().GetMethod("BaseFunc");
method.Invoke(BaseForm, null);
}
int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
OnAutoEvent(i);
i++;
}
public delegate void AutoEventHandler(int i);
public event AutoEventHandler AutoEvent;
public void OnAutoEvent(int i)
{
AutoEvent(i);
}
private void timer2_Tick(object sender, EventArgs e)
{
MethodInfo method = BaseForm.GetType().GetMethod("SetIco");
method.Invoke(BaseForm, null);
}
}
}
浙公网安备 33010602011771号