代码模板 使用代码模板生成代码

很简单的东西 用于批量生产代码

codeTemplate.gif

Buttons LeftThumbX LeftThumbY LeftTrigger RightThumbX RightThumbY RightTrigger

<code class="language-xaml">
<textblock text="{0} = ">


<textblock text="{{Binding {0}}}">




<stackpanel grid.column="0">
   <textblock text="Buttons = ">
   <textblock text="LeftThumbX = ">
   <textblock text="LeftThumbY = ">
   <textblock text="LeftTrigger = ">
   <textblock text="RightThumbX = ">
   <textblock text="RightThumbY = ">
   <textblock text="RightTrigger = ">
</textblock></textblock></textblock></textblock></textblock></textblock></textblock></stackpanel>


<stackpanel grid.column="1">
   <textblock text="{Binding Buttons}">
   <textblock text="{Binding LeftThumbX}">
   <textblock text="{Binding LeftThumbY}">
   <textblock text="{Binding LeftTrigger}">
   <textblock text="{Binding RightThumbX}">
   <textblock text="{Binding RightThumbY}">
   <textblock text="{Binding RightTrigger}">
</textblock></textblock></textblock></textblock></textblock></textblock></textblock></stackpanel>
</textblock></textblock></code>
<code class="language-C#"> namespace CodeTemplate
{
    class Program
    {
        static void Main(string[] args)
        {
            var Input = string.Empty;
            while (true)
            {
                Input = Console.ReadLine();
                switch (Input.ToLower())
                {
                    case "?":
                        Help.Instance.Write();
                        break;
                    case "c":
                    case "cls":
                        Console.Clear();
                        break;
                    case "t":
                    case "template":
                        Console.Clear();
                        Template.Instance.ReadTemplate();
                        Console.Clear();
                        break;
                    default:
                        ClearCurrentConsoleLine();
                        BuildCode.Instance.Read(Input);
                        break;
                }

            }
        }

        public static void ClearCurrentConsoleLine()
        {
            int currentLineCursor = Console.CursorTop - 1;
            Console.SetCursorPosition(0, currentLineCursor);
            Console.Write(new string(' ', Console.WindowWidth));
            Console.SetCursorPosition(0, currentLineCursor);
        }
    }
}

 class Template
    {
        static Template _Instance = new Template();
        public static Template Instance
        {
            get
            {
                return _Instance;
            }

        }
        private Template()
        {

        }

        public void ReadTemplate()
        {
            Console.WriteLine("请输入 Template");
            var input = Console.ReadLine();
            var Template = new StringBuilder();
            while (input != "end" && input != "e")
            {
                Template.Append(input);
                input = Console.ReadLine();
            }
            CodeTemplate = Template.ToString();
        }
        public string CodeTemplate { get; private set; }
    } 




class BuildCode
    {
        static BuildCode _Instance = new BuildCode();
        public static BuildCode Instance
        {
            get
            {
                return _Instance;
            }

        }
        private BuildCode()
        {

        }

        public void Read(string Input)
        {
            try
            {
                Console.WriteLine(Template.Instance.CodeTemplate, Input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    } 





</code>

[修改于 3 年前 - 2017-04-28 14:41:57]

来自:计算机科学 / Windows
2017-4-28 14:20:18
张静茹(作者)
1楼
再举个栗子

变量
LeftThumbX
LeftThumbY
RightThumbX
RightThumbY


模板
public event EventHandler<short> Joypad_{0};
Joypad_{0}?.Invoke(this, JoypadKeyState.Gamepad.{0});



生成结果
public event EventHandler<short> Joypad_LeftThumbX;
public event EventHandler<short> Joypad_LeftThumbY;
public event EventHandler<short> Joypad_RightThumbX;
public event EventHandler<short> Joypad_RightThumbY;


Joypad_LeftThumbX?.Invoke(this, JoypadKeyState.Gamepad.LeftThumbX);
Joypad_LeftThumbY?.Invoke(this, JoypadKeyState.Gamepad.LeftThumbY);
Joypad_RightThumbX?.Invoke(this, JoypadKeyState.Gamepad.RightThumbX);
Joypad_RightThumbY?.Invoke(this, JoypadKeyState.Gamepad.RightThumbY);
折叠评论
加载评论中,请稍候...
折叠评论
2017-4-28 14:26:37
张静茹(作者)
2楼
再举个栗子


变量
Y
None
DPadUp
DPadDown
DPadLeft
DPadRight
Start
Back
LeftThumb
RightThumb
LeftShoulder
RightShoulder
A
B
X



模板
public event EventHandler Joypad_{0};
Joypad_{0}?.Invoke(this, EventArgs.Empty);



结果
public event EventHandler Joypad_Y;
public event EventHandler Joypad_None;
public event EventHandler Joypad_DPadUp;
public event EventHandler Joypad_DPadDown;
public event EventHandler Joypad_DPadLeft;
public event EventHandler Joypad_DPadRight;
public event EventHandler Joypad_Start;
public event EventHandler Joypad_Back;
public event EventHandler Joypad_LeftThumb;
public event EventHandler Joypad_RightThumb;
public event EventHandler Joypad_LeftShoulder;
public event EventHandler Joypad_RightShoulder;
public event EventHandler Joypad_A;
public event EventHandler Joypad_B;
public event EventHandler Joypad_X;



Joypad_Y?.Invoke(this, EventArgs.Empty);
Joypad_None?.Invoke(this, EventArgs.Empty);
Joypad_DPadUp?.Invoke(this, EventArgs.Empty);
Joypad_DPadDown?.Invoke(this, EventArgs.Empty);
Joypad_DPadLeft?.Invoke(this, EventArgs.Empty);
Joypad_DPadRight?.Invoke(this, EventArgs.Empty);
Joypad_Start?.Invoke(this, EventArgs.Empty);
Joypad_Back?.Invoke(this, EventArgs.Empty);
Joypad_LeftThumb?.Invoke(this, EventArgs.Empty);
Joypad_RightThumb?.Invoke(this, EventArgs.Empty);
Joypad_LeftShoulder?.Invoke(this, EventArgs.Empty);
Joypad_RightShoulder?.Invoke(this, EventArgs.Empty);
Joypad_A?.Invoke(this, EventArgs.Empty);
Joypad_B?.Invoke(this, EventArgs.Empty);
Joypad_X?.Invoke(this, EventArgs.Empty);
折叠评论
加载评论中,请稍候...
折叠评论
2017-04-29 01:32:43
3楼

做了个C语言窗口版的


attachment icon codetemp.rar 357.72KB RAR 30次下载

codetemp1.png

codetemp2.png

[修改于 3 年前 - 2017-05-17 17:15:16]

折叠评论
加载评论中,请稍候...
折叠评论
2018-03-02 10:29:27
2018-3-2 10:29:27
张静茹(作者)
4楼
正确方法是使用 Visual Stuidio的 T4 模板  方便 功能强大的多。。。。。。
折叠评论
加载评论中,请稍候...
折叠评论

想参与大家的讨论?现在就 登录 或者 注册

张静茹
学者 机友 笔友
文章
125
回复
1747
学术分
1
2010/12/30注册,1 小时前活动
暂无简介
%7B%22isDisplay%22%3Atrue%7D

仅供内部学术交流或培训使用,请先保存到本地。本内容不代表科创观点,未经原作者同意,请勿转载。

插入资源
全部
图片
视频
音频
附件
全部
未使用
已使用
正在上传
空空如也~
上传中..{{f.progress}}%
处理中..
上传失败,点击重试
{{f.name}}
空空如也~
(视频){{r.oname}}
{{selectedResourcesId.indexOf(r.rid) + 1}}
处理中..
处理失败
插入表情
我的表情
共享表情
Emoji
上传
注意事项
最大尺寸100px,超过会被压缩。为保证效果,建议上传前自行处理。
建议上传自己DIY的表情,严禁上传侵权内容。
点击重试等待上传{{s.progress}}%处理中...已上传
空空如也~
草稿箱
加载中...
此处只插入正文,如果要使用草稿中的其余内容,请点击继续创作。
{{fromNow(d.toc)}}
{{getDraftInfo(d)}}
标题:{{d.t}}
内容:{{d.c}}
继续创作
删除插入插入
{{forum.displayName}}
{{forum.countThreads}}
篇文章,
{{forum.countPosts}}
条回复
{{forum.description || "暂无简介"}}
ID: {{user.uid}}
学术分隐藏
{{submitted?"":"投诉"}}
请选择违规类型:
{{reason.description}}
支持的图片格式:jpg, jpeg, png