using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace 获取指定目录下dll中所有的指定类的方法名
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var targetAssembly = Assembly.Load(File.ReadAllBytes(this.Text));//文件以二进制的方式加载到程序
var targetType = targetAssembly.GetType(className.Text);//定位指定的类
var targetMethods = targetType.GetMethods();//定位指定的方法
listBox1.Items.Clear();
foreach (var targetMethod in targetMethods)
{
if (targetMethod.ToString().Substring(0, 5) != "Void ") continue;
listBox1.Items.Add(targetMethod.Name);
}
if (listBox1.Items.Count!=0)
{
listBox1.SelectedItem = listBox1.Items[listBox1.Items.Count - 1];
methodName.Text = listBox1.Items[listBox1.Items.Count - 1].ToString();
}
readJson();
}
private void listBox1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
methodName.Text = listBox1.SelectedItem.ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
string jsonfile = @"d:\cmd.json";
StringWriter sw = new StringWriter();
JsonTextWriter writer = new JsonTextWriter(sw);
writer.WriteStartObject(); // { (Json数据的大括号左边 )
writer.WritePropertyName("DllName");
writer.WriteValue(dllName.Text);
writer.WritePropertyName("ClassName");
writer.WriteValue(className.Text);
writer.WritePropertyName("MethodName");
writer.WriteValue(methodName.Text);
writer.WriteEndObject();//}
StreamWriter wtyeu = new StreamWriter(jsonfile);
wtyeu.Write(sw);
wtyeu.Flush();
wtyeu.Close();
readJson();
}
void readJson()
{
string jsonfile = @"d:\cmd.json";
using (StreamReader file = File.OpenText(jsonfile))
{
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject o = (JObject)JToken.ReadFrom(reader);
label2.Text = "当前的方法名是:" + o["MethodName"].ToString();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
button1_Click( sender, e);
}
}
}
|