公司给的一个小的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\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 = ; //读取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(, method.IndexOf("Name") - );
string methodName = method.Substring(method.IndexOf("Name:") + , method.Length - method.IndexOf("Name:") - );
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() + " " + rec.GetString() + " " + rec.GetString());
methods.Add(new method
{
id = "" + rec.GetInt32(),
type = "" + rec.GetString(),
name = "" + rec.GetString()
});
}
myconn.Close();
return methods;
}
}
class method
{
public string id { get; set; }
public string type { get; set; }
public string name { get; set; } } }

C#简单入门的更多相关文章

  1. 用IntelliJ IDEA创建Gradle项目简单入门

    Gradle和Maven一样,是Java用得最多的构建工具之一,在Maven之前,解决jar包引用的问题真是令人抓狂,有了Maven后日子就好过起来了,而现在又有了Gradle,Maven有的功能它都 ...

  2. [原创]MYSQL的简单入门

    MYSQL简单入门: 查询库名称:show databases; information_schema mysql test 2:创建库 create database 库名 DEFAULT CHAR ...

  3. Okio 1.9简单入门

    Okio 1.9简单入门 Okio库是由square公司开发的,补充了java.io和java.nio的不足,更加方便,快速的访问.存储和处理你的数据.而OkHttp的底层也使用该库作为支持. 该库极 ...

  4. emacs最简单入门,只要10分钟

    macs最简单入门,只要10分钟  windwiny @2013    无聊的时候又看到鼓吹emacs的文章,以前也有几次想尝试,结果都是玩不到10分钟就退出删除了. 这次硬着头皮,打开几篇文章都看完 ...

  5. 【java开发系列】—— spring简单入门示例

    1 JDK安装 2 Struts2简单入门示例 前言 作为入门级的记录帖,没有过多的技术含量,简单的搭建配置框架而已.这次讲到spring,这个应该是SSH中的重量级框架,它主要包含两个内容:控制反转 ...

  6. Docker 简单入门

    Docker 简单入门 http://blog.csdn.net/samxx8/article/details/38946737

  7. Springmvc整合tiles框架简单入门示例(maven)

    Springmvc整合tiles框架简单入门示例(maven) 本教程基于Springmvc,spring mvc和maven怎么弄就不具体说了,这边就只简单说tiles框架的整合. 先贴上源码(免积 ...

  8. git简单入门

    git简单入门 标签(空格分隔): git git是作为程序员必备的技能.在这里就不去介绍版本控制和git产生的历史了. 首先看看常用的git命令: git init git add git comm ...

  9. 程序员,一起玩转GitHub版本控制,超简单入门教程 干货2

    本GitHub教程旨在能够帮助大家快速入门学习使用GitHub,进行版本控制.帮助大家摆脱命令行工具,简单快速的使用GitHub. 做全栈攻城狮-写代码也要读书,爱全栈,更爱生活. 更多原创教程请关注 ...

  10. Web---演示Servlet的相关类、表单多参数接收、文件上传简单入门

    说明: Servlet的其他相关类: ServletConfig – 代表Servlet的初始化配置参数. ServletContext – 代表整个Web项目. ServletRequest – 代 ...

随机推荐

  1. CF AIM Tech Round 3 (Div. 2) D - Recover the String

    模拟 首先可以求出 0 和 1 的个数 之后按照01 10 的个数贪心安排 细节太多 错的都要哭了 #include<bits/stdc++.h> using namespace std; ...

  2. java暴力递归回溯算法

    今天这个问题是我之前一直想解决的,还记得以前第一次上蓝桥杯的课的时候,也就是大一高数期中模拟考试那天,下午去上蓝桥杯课,遇到这道题,当时写了写,根本没有思路,然后就给大一的模拟考试去了.印象深刻啊,一 ...

  3. JAVA流式布局管理器--JAVA基础

    JAVA流式布局管理器的使用: FlowLayoutDeme.java: import java.awt.*;import javax.swing.*;public class FlowLayoutD ...

  4. 在实训时做的项目出现的ajax json数据传送的问题

    json数据在前后端数据交互的时候非常常见,但是大部分人对json都么有系统的学习过,所以就会出现一些很简单的问题却要非很大劲去解决. 在用json传递数据的时候属性必须用双引号括住,一般如果在进行字 ...

  5. C++标准库bitset类型(简单使用方法)

    转自此人博客 ```cpp #include<bister> using std::bitset; ``` 一句话定义:可自定义位数,用作记录二进制的数据类型. 一,定义和初始化 ```c ...

  6. java中获取项目在tomcat目录下的路径方法

    HttpServletRequest request //获取的是ROOT项目在tomcat下的路径 方法1: String path = request.getSession().getServle ...

  7. mysql的conv的用法

    这次的ctf比赛用到这个函数,这里记录一下 题目禁了ascii , ord 那就使用conv 这个函数是用来将字符转换进制的,例如将a转成ASCII码(换个说法就是将16进制的a换成10进制) 那就直 ...

  8. 禁被ping 软件漏洞升级

    禁被ping:echo “net.ipv4.icmp_echo_ignore_all=1”  /etc/sysctl.conf 软件漏洞升级:yum install openssh bash -y

  9. js与后台交互详述(入门篇)

    很多新手前端在初期学习的时候往往把注意力放在如何编写页面,如何编写效果上,群里有个朋友问我js是如何与后台交互的,我简单的说一下. 首先需要知道两个东西,一个是客户端,一个是服务器,客户端其实就是我们 ...

  10. TP5 路由使用

    这里可以直接从第四条开始看 原文http://www.upwqy.com/details/12.html 1 文档链接 tp5路由官方文档  https://www.kancloud.cn/manua ...