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 – 代 ...
随机推荐
- 芝麻HTTP:Ansible扩展
Ansible简介 Ansible是由Python开发的一个运维工具,因为工作需要接触到Ansible,经常会集成一些东西到Ansible,所以对Ansible的了解越来越多. 那Ansible到底是 ...
- 【转】MySQL乐观锁在分布式场景下的实践
背景 在电商购物的场景下,当我们点击购物时,后端服务就会对相应的商品进行减库存操作.在单实例部署的情况,我们可以简单地使用JVM提供的锁机制对减库存操作进行加锁,防止多个用户同时点击购买后导致的库存不 ...
- [BZOJ4736]温暖会指引我们前行
BZOJ(BZOJ上的是什么鬼...) UOJ 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很低. 小 ...
- WC2006水管局长(加强)
倒过来就变成了加边 然后就直接LCT # include <stdio.h> # include <stdlib.h> # include <iostream> # ...
- weblogic 服务器部署SSL证书
一.证书介绍 1.需要的证书 生产需要的证数如下: 即客户提供的证数: L1Croot.crt,L1Cchain.crt,entrustcert.crt,server,jks 证书清单: L1Croo ...
- c++ STL容器适配器
一.标准库顺序容器适配器的种类 标准库提供了三种顺序容器适配器:queue(FIFO队列).priority_queue(优先级队列).stack(栈) 二.什么是容器适配器 &q ...
- 【前端单元测试入门01】Mocha与chai
Mocha 的简介 Mocha是流行的JavaScript测试框架之一,通过它添加和运行测试,从而保证代码质量 Mocha 的安装与配置 全局安装Mocha npm install -g mocha ...
- Vuejs实例-使用vue-cli创建项目
1,首先从官方网站下载安装Node.js,建议使用6.x版本,同时也会一并安装npm工具,npm>3.10以上. 2,npm安装很慢(国外服务器),所以一般推荐使用npm淘宝镜像cnpm,先安装 ...
- 如何为开发项目编写规范的README文件(windows),此文详解
为什么要写这篇博客? 其实我是一个入坑已经半年的程序员,因为不是计算机专业,只能自己摸索,所以我深知博客的重要性.每次我的学习笔记啊,项目的,面试题啊,有的,只要有时间,我肯定上传上来,一方面自己可以 ...
- 【Unity与23种设计模式】抽象工厂模式(Abstract Factory)
GoF中定义: "提供一个能够建立整个类群组或有关联的对象,而不必指明它们的具体类." 意思就是 根据不同的执行环境产生不同的抽象类子类 抽象工厂模式经常在面试中会涉及到 下面的例 ...