在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
公司给的一个小的practice C# vs2017 Stage 1 (cmd) Stage 2 (cmd) Stage 3 (cmd)
Stage1 解析一个dll并取出里面所有的public方法,写入到txt中。先解析,用反射即可,这里要注意,因为有的dll是有其他依赖所以可能会无法解析,这里可以选择自己写一个dll,然后尝试解析它。解析之后将其中的public方法写入txt中。 using System; using System.Reflection; using System.IO; namespace Stage1 { class Program { //解析dll,将public方法写入txt static void Main(string[] args) { StreamWriter sw = new StreamWriter(@"D:\\C#source\result.txt"); ; //获取assembly Assembly asb = Assembly.LoadFrom(@"D:\C#source\Stage1\Temp\bin\Debug\netcoreapp2.0\Temp.dll"); //获取module Module[] modules = asb.GetModules(); foreach (Module module in modules) { //获取type Type[] types = module.GetTypes(); foreach (Type type in types) { //获取method MethodInfo[] mis = type.GetMethods(); foreach (MethodInfo mi in mis) { sw.Write("Type:" + mi.ReturnType + " Name:" + mi.Name+ "\r\n"); } } } sw.Close(); Console.ReadKey(); } } }
Stage2 创建一个数据库表,将txt中的方法读入数据库表中。这里要注意的就是,添加依赖的时候直接从NuGet中选择就好,因为以前写java比较多,这个就类似于java里的maven工具,自动添加依赖而不用手动添加。 using MySql.Data.MySqlClient; using System; using System.IO; using System.Text; namespace Stage2 { class Program { //txt中方法写入数据库 static void Main(string[] args) { MySqlConnection myconn = new MySqlConnection("Host =localhost;Database=dllmethod;Username=root;Password=314159"); myconn.Open(); MySqlCommand mycom = null; int index = 1; //读取txt StreamReader sr = new StreamReader(@"D:\\C#source\result.txt", Encoding.Default); String line; while ((line = sr.ReadLine()) != null) { string sql = string.Format("insert into publicmethod(id,type,name) values( "); //处理line string method = line.ToString(); string methodType = method.Substring(5, method.IndexOf("Name") - 5); string methodName = method.Substring(method.IndexOf("Name:") + 5, method.Length - method.IndexOf("Name:") - 5); Console.WriteLine(methodType); Console.WriteLine(methodName); sql = sql + index + ",\"" + methodType + "\",\"" + methodName + "\")"; mycom= new MySqlCommand(sql, myconn); mycom.ExecuteNonQuery(); index++; } Console.ReadKey(); myconn.Close(); } } }
Stage3 把public方法从数据库中都出来,一个导成json格式,一个导成xml格式,数据与json数据的转换只需要序列化与反序列化即可,同时需要依赖一个Json Newtonsoft包,xml格式只需要一个XML包即可。linq读取json保存的txt中,将json中的数据反序列化取出即可。 using MySql.Data.MySqlClient; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml.Linq; namespace Stage3 { class Program { static void Main(string[] args) { ToJson(); ToXML(); LinqToTxt(); Console.ReadKey(); } //linq读取json,保存到txt中 static void LinqToTxt() { //从json中读出 string fp = "D:\\C#source/MyJSON.json"; string json=File.ReadAllText(fp); Console.WriteLine(JsonConvert.DeserializeObject(json)); //写入txt中 StreamWriter sw = new StreamWriter("D:\\C#source/JsonToTxt.txt"); string w = JsonConvert.DeserializeObject(json).ToString(); sw.Write(w); sw.Close(); } //生成json static void ToJson() { List<method> methods = getMethodFromDB(); string fp = "D:\\C#source/MyJSON.json"; if (!File.Exists(fp)) // 判断是否已有相同文件 { FileStream fs1 = new FileStream(fp, FileMode.Create, FileAccess.ReadWrite); fs1.Close(); } File.WriteAllText(fp, JsonConvert.SerializeObject(methods)); Console.WriteLine(); } //生成xml static void ToXML() { List<method> methods=getMethodFromDB(); XDocument document = new XDocument(); XElement root = new XElement("Public"); XElement book = null; foreach (method method in methods) { book = new XElement("Method"+method.id); book.SetElementValue("type", method.type); book.SetElementValue("name", method.name); root.Add(book); } root.Save("d:\\C#source/MyXML.xml"); Console.WriteLine(); } //获取数据库中内容 static List<method> getMethodFromDB() { List<method> methods = new List<method>(); MySqlConnection myconn = new MySqlConnection("Host =localhost;Database=dllmethod;Username=root;Password=314159"); myconn.Open(); MySqlCommand sqlCmd = new MySqlCommand(); sqlCmd.Connection = myconn; sqlCmd.CommandText = "select * from publicmethod"; MySqlDataReader rec = sqlCmd.ExecuteReader(); //读取publicmethod表中的内容到methods中 while (rec.Read()) { Console.WriteLine(" " + rec.GetInt32(0) + " " + rec.GetString(1) + " " + rec.GetString(2)); methods.Add(new method { id = "" + rec.GetInt32(0), type = "" + rec.GetString(1), name = "" + rec.GetString(2) }); } myconn.Close(); return methods; } } class method { public string id { get; set; } public string type { get; set; } public string name { get; set; } } }
|
请发表评论