zoukankan      html  css  js  c++  java
  • C#简单入门

    公司给的一个小的practice

    C# vs2017

    Stage 1 (cmd)
    1. Parse the dll (reflection)
    2. Write all the public methods to a txt file (io)

    Stage 2 (cmd)
    1. Create a local database table
    2. Read the txt file about the methods
    3. Store the methods to datatable (ado.net)

    Stage 3 (cmd)
    1. Read the methods from database
    2. generate two files to store the methods (one by json format, one by xml format)
    3. Use (linq) to read the json file, count the public methods those under a dll, store it to a txt file

    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
    esult.txt"); ;
                //获取assembly
                Assembly asb = Assembly.LoadFrom(@"D:C#sourceStage1TempinDebug
    etcoreapp2.0Temp.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+ "
    ");
                        }
                    }
                }
                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
    esult.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; }
            
        }
    
    }
  • 相关阅读:
    BurnInTest 设置屏蔽错误显示
    linux内核之模块参数及导出符号
    小程序设置背景图片
    微信小程序wxss样式文件中引用iconfont素材
    mac或linux中打开.bashrc,编辑完之后如何保存退出
    git clone 远程分支内容
    vue 自定义指令
    Vue笔记
    uniapp 小程序上获取不同机型 距离
    git 添加.gitignore文件不生效
  • 原文地址:https://www.cnblogs.com/GoForMyDream/p/8516004.html
Copyright © 2011-2022 走看看