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 – 代 ...
随机推荐
- Flex动态获取数据,服务中断报错
1.错误原因 2.错误原因 由上面提示可知,软件引起的链接中断,导致出错 3.解决办法 检查数据库链接,重新启动服务
- cookies、sessionStorage、和localStorage的区别。
为什么会有cookie和session? 我们都知道http是无状态的协议无连接的,客户每次在读取web页面时服务器都会打开新的会话.服务器不会自动维护客户上下文的信息,那么session就是一种保存 ...
- springboot集成Actuator
Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...
- Html行内元素和块级元素
1.关于行内元素和块状元素的说明 根据CSS规范的规定,每一个网页元素都有一个display属性,用于确定该元素的类型,每一个元素都有默认的display属性值,比如div元素,它的默认display ...
- Oracle11g客户端client的下载与安装
下载地址: http://www.oracle.com/technetwork/database/enterprise-edition/downloads/112010-win64soft-09446 ...
- BZOJ第1页养成计划
嗯,用这篇博客当一个目录,方便自己和学弟(妹?)们查阅.不定期更新. BZOJ1000 BZOJ1001 BZOJ1002 BZOJ1003 BZOJ1004 BZOJ1005 ...
- JAVA面试一
ORACLE分页 -- 要求根据年龄排序后的 第三 行到第6 行的数据 (?) 分页语句 select t.* from ( select t1. *, rownum num from ( selec ...
- mac php 版本切换
注意:要求所有php版本都是由brew安装 一.使用brew安装php多版本方法 # brew install php56 # brew install php70 二.安装切换工具 # brew i ...
- php表单提交时获取不到post数据的解决方法
找到了一位博主的方法完美解决,链接如下: http://blog.csdn.net/whd526/article/details/53263181
- wpf研究之道-ProgressBar(进度条)控件
ProgressBar控件,非常有用.它在什么情况下有用呢?如何使用?带着这两个问题,我们探讨下. 如果程序需要很长时间来运行,用户在不知道的情况下,以为程序已经"卡死"了,没有响 ...