公司给的一个小的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. tomcat原理(一)server.xml中的host虚拟主机的理解

    一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件. 如果想修改Tomcat服务器的启动端口,则可以在server.xml ...

  2. Python Web-第四周-Programs that Surf the Web(Using Python to Access Web Data)

    1.Understanding HTML 1.最简单的爬虫 import urllib fhand=urllib.urlopen('http://www.dr-chuck.com/page1.htm' ...

  3. Centos7.2 搭建Lamp服务器以及迁移WordPress个人博客详细过程

    其实自己的博客搭了有段时间了,但是由于自己不太确定是不是一定要用wd的框架,以及实验室公网服务器的不稳定,就一直荒废着. 今天偶然间看到了腾讯云对于学生的优惠活动,毕业之前每月只要8元的云服务器(就算 ...

  4. Python基础__Python序列基本类型及其操作(1)

    本节考虑的Python的一个中要的内置对象序列, 所谓的序列即一个有序对象的集合.这里的对象可以是数字.字符串等.根据功能的不同将序列分为字符串.列表.元组,本文将以下这几种对象做一些介绍. 一. 字 ...

  5. ThreadPoolExecutor源码分析

    ThreadPoolExecutor是Java自带线程池FixedThreadPool(固定大小). SingleThreadExecutor(单线程).CacheThreadPool (无限大)的具 ...

  6. JDK 9.0.4安装过程

    因为种种问题,怀疑是因为JDK版本不对劲,于是打算将JDK重新搞一下. 不看不知道,看了吓一跳,我的笔记本里现在起码有5.6甚至更多个JDK,JRE,并且由于年久失修,我也不知道这些东西怎么装上去的, ...

  7. 【BZOJ3730】震波(动态点分治)

    [BZOJ3730]震波(动态点分治) 题面 BZOJ 题意 给定一棵树, 每次询问到一个点的距离\(<=K\)的点的权值之和 动态修改权值, 强制在线 题解 正常的\(DP\)??? 很简单呀 ...

  8. 【BZOJ1018】堵塞的交通(线段树)

    [BZOJ1018]堵塞的交通(线段树) 题面 Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可 以被看成是一个2行C列的矩形网 ...

  9. Hive 自定义函数

    hive 支持自定义UDF,UDTF,UDAF函数 以自定义UDF为例: 使用一个名为evaluate的方法 package com.hive.custom; import org.apache.ha ...

  10. 在hive下使用dual伪表

    [hive@nn1 ~]$ touch dual.txt[hive@nn1 ~]$ echo 'X' >dual.txt hive> load data local inpath '/ho ...