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\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#简单入门的更多相关文章
- 用IntelliJ IDEA创建Gradle项目简单入门
Gradle和Maven一样,是Java用得最多的构建工具之一,在Maven之前,解决jar包引用的问题真是令人抓狂,有了Maven后日子就好过起来了,而现在又有了Gradle,Maven有的功能它都 ...
- [原创]MYSQL的简单入门
MYSQL简单入门: 查询库名称:show databases; information_schema mysql test 2:创建库 create database 库名 DEFAULT CHAR ...
- Okio 1.9简单入门
Okio 1.9简单入门 Okio库是由square公司开发的,补充了java.io和java.nio的不足,更加方便,快速的访问.存储和处理你的数据.而OkHttp的底层也使用该库作为支持. 该库极 ...
- emacs最简单入门,只要10分钟
macs最简单入门,只要10分钟 windwiny @2013 无聊的时候又看到鼓吹emacs的文章,以前也有几次想尝试,结果都是玩不到10分钟就退出删除了. 这次硬着头皮,打开几篇文章都看完 ...
- 【java开发系列】—— spring简单入门示例
1 JDK安装 2 Struts2简单入门示例 前言 作为入门级的记录帖,没有过多的技术含量,简单的搭建配置框架而已.这次讲到spring,这个应该是SSH中的重量级框架,它主要包含两个内容:控制反转 ...
- Docker 简单入门
Docker 简单入门 http://blog.csdn.net/samxx8/article/details/38946737
- Springmvc整合tiles框架简单入门示例(maven)
Springmvc整合tiles框架简单入门示例(maven) 本教程基于Springmvc,spring mvc和maven怎么弄就不具体说了,这边就只简单说tiles框架的整合. 先贴上源码(免积 ...
- git简单入门
git简单入门 标签(空格分隔): git git是作为程序员必备的技能.在这里就不去介绍版本控制和git产生的历史了. 首先看看常用的git命令: git init git add git comm ...
- 程序员,一起玩转GitHub版本控制,超简单入门教程 干货2
本GitHub教程旨在能够帮助大家快速入门学习使用GitHub,进行版本控制.帮助大家摆脱命令行工具,简单快速的使用GitHub. 做全栈攻城狮-写代码也要读书,爱全栈,更爱生活. 更多原创教程请关注 ...
- Web---演示Servlet的相关类、表单多参数接收、文件上传简单入门
说明: Servlet的其他相关类: ServletConfig – 代表Servlet的初始化配置参数. ServletContext – 代表整个Web项目. ServletRequest – 代 ...
随机推荐
- freemarker写select组件(二十二)
一,讲解一 1.宏定义 <#macro select id datas> <select id="${id}" name="${id}"> ...
- CSS3 2D、3D转换
2D转换方法:transform().rotate().scale().skew().matrix() 3D转换方法:rotateX().rotateY() 1.示例代码 <!DOCTYPE h ...
- Jmeter_从jdbc请求的响应中获取参数做关联
在之前的文章-参数关联中,留个一个小尾巴,这里补充一下 http://www.cnblogs.com/Zfc-Cjk/p/8295495.html 1:从sql表中将需要取的数据查出来 2:我们需要把 ...
- 【BZOJ1216】操作系统(堆,模拟)
[BZOJ1216]操作系统(堆,模拟) 题面 题目描述 写一个程序来模拟操作系统的进程调度.假设该系统只有一个CPU,每一个进程的到达时间,执行时间和运行优先级都是已知的.其中运行优先级用自然数表示 ...
- SpringMVC入门就这么简单
什么是SpringMVC? SpringMVC是Spring家族的一员,Spring是将现在开发中流行的组件进行组合而成的一个框架!它用在基于MVC的表现层开发,类似于struts2框架 为什么要使用 ...
- C++学习-7
1.面向过程是:数据与操作分离,数据容易被意外修改 2.面向过程通过私有化的权限进行数据封装 3.类型后辍:类名 operator "" _XXXX(int data) 增加后缀 ...
- oracle 12c 安装指南(各种问题总结)
下一学期要学习数据库的知识,趁着敲代码之余,安装著名的oracle来放松一下 1.首先从官网下载安装包http://www.oracle.com/technetwork/database/enterp ...
- API网关系列之Kong的介绍以及安装
一.API网关产生背景 在微服务的架构中,一个大的应用会被拆分成多个小的单一的服务提供出来,这些小的服务有自己的处理,有自己的数据库(也可以共用),也许语言也是不一样的,他们可以部署在一个或多个服务器 ...
- ngrx/store effects 使用总结1:计数器
本教程案例github:https://github.com/axel10/ngrx_demo-counter-and-list angular2+ 的学习成本应该是三大框架中最高的一个,教程及案例稀 ...
- .NET Core开源API网关 – Ocelot中文文档
Ocelot是一个用.NET Core实现并且开源的API网关,它功能强大,包括了:路由.请求聚合.服务发现.认证.鉴权.限流熔断.并内置了负载均衡器与Service Fabric.Butterfly ...