/// <summary> /// 生成文件的 /// </summary> /// <param name="calssName"></param> public void create(string calssName) { //獲取程式集 var createClass = Assembly. ...
/// <summary> /// 生成文件的 /// </summary> /// <param name="calssName"></param> public void create(string calssName) { //獲取程式集 var createClass = Assembly.Load("CloudInnovate.Backstage.Entity"); //反射出所有的類 List<Type> ts = createClass.GetTypes().ToList(); //迴圈生成 ts.ForEach(x => { //x.Namespace獲取命名空間 var ss = "using " + x.Namespace + ";\n" + "using CloudInnovate.CoreFramework." + calssName + ";\n" + "using System;\n" + "using System.Collections.Generic;\n" + "using System.Text;\n\n" + "namespace CloudInnovate.Backstage.I" + calssName + "\n" + "{\n" + "\tpublic interface I" + x.Name.Substring(0, x.Name.Length - 6) + "" + calssName + " : IBase" + calssName + "<" + x.Name + ">\n" + "\t{\n" + "\t}\n" + "}\n"; //創建文件夾 if (!Directory.Exists(@"C:\Users\Administrator\Desktop\I" + calssName + "")) { Directory.CreateDirectory(@"C:\Users\Administrator\Desktop\I" + calssName + ""); } //創建文件夾 if (!Directory.Exists(@"C:\Users\Administrator\Desktop\I" + calssName + @"\" + x.Name.Substring(0, x.Name.Length - 6) + "")) { Directory.CreateDirectory(@"C:\Users\Administrator\Desktop\I" + calssName + @"\" + x.Name.Substring(0, x.Name.Length - 6) + ""); } //保存 開啟文件流 using (FileStream fs = new FileStream(@"C:\Users\Administrator\Desktop\I" + calssName + @"\" + x.Name.Substring(0, x.Name.Length - 6) + @"\I" + x.Name.Substring(0, x.Name.Length - 6) + "" + calssName + ".cs", FileMode.Create)) { //文本寫入 開啟讀寫流 using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(ss); } } }); }
最近在項目中使用了倉儲模式有些代碼太過於重覆了所以想寫個生成。
本來是想用T4模板的,但是感覺T4不好用,所以就是使用了讀寫了直接寫入吧。
主要就是想記錄一下,好記性不如爛筆頭。所以想博客記錄一下。