文件批量生成IO流读写

/// <summary>
        /// 生成文件的
        /// </summary>
        /// <param name="calssName"></param>
        public void create(string calssName)
        {
            //获取程序集
            var createClass = Assembly.Load("Entity");
            //反射出所有的类
            List<Type> ts = createClass.GetTypes().ToList();
            //循环生成
            ts.ForEach(x =>
            {
                //x.Namespace获取命名空间
                var ss = "using " + x.Namespace + ";\n" +
                         "using CoreFramework." + calssName + ";\n" +
                         "using System;\n" +
                         "using System.Collections.Generic;\n" +
                         "using System.Text;\n\n" +

                         "namespace 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\Desktop\I" + calssName + ""))
                {
                    Directory.CreateDirectory(@"C:\Users\Desktop\I" + calssName + "");
                }
                //创建文件夹
                if (!Directory.Exists(@"C:\Users\Desktop\I" + calssName + @"\" + x.Name.Substring(0, x.Name.Length - 6) + ""))
                {
                    Directory.CreateDirectory(@"C:\Users\Desktop\I" + calssName + @"\" + x.Name.Substring(0, x.Name.Length - 6) + "");
                }
                //保存 开启文件流
                using (FileStream fs = new FileStream(@"C:\Users\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不好用,所以就是使用了读写了直接写入吧。

主要就是想记录一下,好记性不如烂笔头。所以想博客记录一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值