C # Easy Start

Source: Internet
Author: User
Tags tojson

The company gave a small practice

C # vs2017

Stage 1 (CMD)
1. Parse the DLL (reflection)
2. Write all, 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)

Stage 3 (CMD)
1. Read the methods from database
2. Generate 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 parses a DLL and takes out all the public methods inside it and writes it to TXT. First parse, with reflection can, here to note, because some DLLs have other dependencies so may be unable to parse, here you can choose to write a DLL, and then try to parse it. After parsing, the public method is written to txt.

usingSystem;usingSystem.Reflection;usingSystem.IO;namespacestage1{classProgram {//parses the DLL and writes the public method to the TXT        Static voidMain (string[] args) {StreamWriter SW=NewStreamWriter (@"D:\\c#source\result.txt"); ; //Get AssemblyAssembly ASB = Assembly.LoadFrom (@"D:\c#source\stage1\temp\bin\debug\netcoreapp2.0\temp.dll"); //Get ModuleModule[] Modules =ASB.            GetModules (); foreach(Module moduleinchmodules) {                //Gets the typetype[] Types =module.                GetTypes (); foreach(Type typeinchtypes) {                    //Get Methodmethodinfo[] Mis=type.                    GetMethods (); foreach(MethodInfo miinchmis) {SW. Write ("Type:"+ mi. ReturnType +"Name:"+ mi. name+"\ r \ n"); }}} SW.            Close ();        Console.readkey (); }    }}

Stage2 creates a database table that reads the methods in TXT into a database table. It is important to note that it is best to choose from NuGet when adding dependencies, since it is much more written than Java, which is similar to the Maven tool in Java, adding dependencies automatically instead of adding them manually.

usingMySql.Data.MySqlClient;usingSystem;usingSystem.IO;usingSystem.Text;namespacestage2{classProgram {//txt method to write to the database        Static voidMain (string[] args) {Mysqlconnection myconn=NewMysqlconnection ("Host =localhost;database=dllmethod; Username=root; password=314159"); MyConn.            Open (); Mysqlcommand mycom=NULL; intindex =1; //Read txtStreamReader sr =NewStreamReader (@"D:\\c#source\result.txt", Encoding.default);            String Line;  while(line = Sr. ReadLine ())! =NULL)            {                stringsql =string. Format ("INSERT INTO Publicmethod (id,type,name) VALUES ("); //Handling Line                stringMETHOD =Line .                ToString (); stringMethodtype = method. Substring (5, method. IndexOf ("Name") -5); stringMethodName = method. Substring (method. IndexOf ("Name:") +5, method. Length-method. IndexOf ("Name:") -5);                Console.WriteLine (Methodtype);                Console.WriteLine (MethodName); SQL= SQL + index +",\""+ Methodtype +"\",\""+ MethodName +"\")"; Mycom=Newmysqlcommand (SQL, myconn); Mycom.                ExecuteNonQuery (); Index++;                        } console.readkey (); MyConn.        Close (); }    }}

Stage3 the public method from the database, one in JSON format, one in XML format, the data and JSON data conversion only need to serialize and deserialize, and need to rely on a JSON newtonsoft package, XML format requires only one XML package. LINQ reads the JSON-saved TXT and deserializes the data in the JSON.

usingMySql.Data.MySqlClient;usingNewtonsoft.json;usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Text;usingSystem.Xml.Linq;namespacestage3{classProgram {Static voidMain (string[] args)            {ToJson ();            ToXML ();            Linqtotxt ();        Console.readkey (); }        //LINQ read JSON, saved to txt        Static voidLinqtotxt () {//read from JSON            stringFP ="D:\\c#source/myjson.json"; stringjson=File.readalltext (FP);            Console.WriteLine (Jsonconvert.deserializeobject (JSON)); //write in txtStreamWriter SW =NewStreamWriter ("D:\\c#source/jsontotxt.txt"); stringW =Jsonconvert.deserializeobject (JSON).            ToString (); Sw.            Write (w); Sw.        Close (); }        //Generate JSON        Static voidToJson () {List<method> methods =Getmethodfromdb (); stringFP ="D:\\c#source/myjson.json"; if(! File.exists (FP))//determine if you already have the same file{FileStream FS1=NewFileStream (FP, FileMode.Create, FileAccess.ReadWrite); FS1.            Close ();                        } file.writealltext (FP, Jsonconvert.serializeobject (methods));        Console.WriteLine (); }        //Generate XML        Static voidToXML () {List<method> methods=Getmethodfromdb (); XDocument Document=NewXDocument (); XElement Root=NewXElement (" Public"); XElement Book=NULL; foreach(Method methodinchmethods) { Book=NewXElement ("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 (); }        //get the contents of the database        StaticList<method>Getmethodfromdb () {List<method> methods =NewList<method>(); Mysqlconnection myconn=NewMysqlconnection ("Host =localhost;database=dllmethod; Username=root; password=314159"); MyConn.            Open (); Mysqlcommand SQLCMD=NewMysqlcommand (); Sqlcmd.connection=myconn; Sqlcmd.commandtext="SELECT * from Publicmethod"; Mysqldatareader Rec=Sqlcmd.executereader (); //reads the contents of the Publicmethod table into the methods             while(rec. Read ()) {Console.WriteLine (" "+ Rec. GetInt32 (0) +" "+ Rec. GetString (1) +" "+ Rec. GetString (2)); Methods. ADD (NewMethod {ID=""+ Rec. GetInt32 (0), type=""+ Rec. GetString (1), name=""+ Rec. GetString (2)                }); } myconn.            Close (); returnmethods; }    }    classMethod { Public stringID {Get;Set; }  Public stringType {Get;Set; }  Public stringName {Get;Set; } }}

C # Easy Start

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.