Asp.Net Razor的使用之解析txt模板

  • binGe博客
  • C#
  • 2022/1/21 10:35:00
  • 人已阅读
简介Engine.Razor.RunCompile(template, key, model.GetType(), model)可解析txt里的C#语法
using System;
using System.Collections.Generic;
using System.Linq;
using SqlSugar;
namespace @Model.name_space
{
    /// <summary>
    /// @((Model.Description+"").Replace("\r","").Replace("\n",""))
    ///</summary>
    [SugarTable("@(Model.TableName)")]
    public class @(Model.ClassName)
    {
@foreach (var item in Model.PropertyGens)
{
    var isPrimaryKey = item.IsPrimaryKey ? ",IsPrimaryKey = true" : "";
    var isIdentity = item.IsIdentity ? ",IsIdentity = true" : "";
    var isNull=(item.IsNullable&&item.Type!="string"&&item.IsSpecialType==false&&item.Type!="byte[]")?"?":"";
    var isIgnore=(item.IsIgnore?",IsIgnore = true":"");
    var isJson=(item.CodeType.StartsWith("json")?",IsJson= true":"");

    var newPropertyName=item.PropertyName; //这里可以用C#处理 实体属性的显式格式
    //想和数据库一样就用 newPropertyName=item.DbColumnName
    if(System.Text.RegularExpressions.Regex.IsMatch(newPropertyName.Substring(0,1), "[0-9]"))
    {
        newPropertyName="_"+newPropertyName;//处理属性名开头为数字情况
    }
    if(newPropertyName==Model.ClassName)
    {
        newPropertyName="_"+newPropertyName;//处理属性名不能等于类名
    }


    var desc=(item.Description+"").Replace("\r","").Replace("\n","");//处理换行

    if(isIgnore!="")
    {
       isPrimaryKey =isIdentity =isNull="";
     }
        @:/// <summary>
        @:/// @(desc) 
        @if(item.DefaultValue!=null)
        {
        @:/// 默认值: @Raw(item.DefaultValue)
        }
        @:///</summary>
        @: [SugarColumn(ColumnName="@item.DbColumnName" @(isPrimaryKey) @(isIdentity) @(isIgnore) @(isJson))]
        @: public @Raw(item.Type)@isNull @newPropertyName { get; set; }
    }
    }
}

文章评论

评论
  • 消灭零回复
Top