1. C# / Говнокод #26803

    0

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    <#@ output extension=".cs" #>
    <#@ assembly name="System.Xml" #>
    <#
     System.Xml.XmlDocument configurationData = ...; // Read a data file here.
    #>
    namespace Fabrikam.<#= configurationData.SelectSingleNode("jobName").Value #>
    {
      ... // More code here.
    }

    T4
    Design time templates define part of the source code and other resources of your application. Typically you use several templates that read the data in a single input file or database, and generate some of your .cs, .vb, or other source files. Each template generates one file. They are executed within Visual Studio or MSBuild.

    https://docs.microsoft.com/en-us/visualstudio/modeling/code-generation-and-t4-text-templates?view=vs-2019

    Запостил: MAKAKA, 11 Июля 2020

    Комментарии (6) RSS

    • генератор говнокода
      Ответить
      • Приведи полный реальный пример такого генератора.
        Ответить
        • <#@ template language="C#" debug="false" hostspecific="true"#>
          <#@ include file="EF6.Utility.CS.ttinclude"#><#@ 
           output extension=".cs"#><#
          
          const string inputFile = @"Model1.edmx";
          var textTransform = DynamicTextTransformation.Create(this);
          var code = new CodeGenerationTools(this);
          var ef = new MetadataTools(this);
          var typeMapper = new TypeMapper(code, ef, textTransform.Errors);
          var	fileManager = EntityFrameworkTemplateFileManager.Create(this);
          var itemCollection = new EdmMetadataLoader(textTransform.Host, textTransform.Errors).CreateEdmItemCollection(inputFile);
          var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef);
          
          if (!typeMapper.VerifyCaseInsensitiveTypeUniqueness(typeMapper.GetAllGlobalItems(itemCollection), inputFile))
          {
              return string.Empty;
          }
          
          WriteHeader(codeStringGenerator, fileManager);
          
          foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
          {
              fileManager.StartNewFile(entity.Name + ".cs");
              BeginNamespace(code);
          #>
          <#=codeStringGenerator.UsingDirectives(inHeader: false)#>
          <#=codeStringGenerator.EntityClassOpening(entity)#>
          {
          <#
              var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(entity);
              var collectionNavigationProperties = typeMapper.GetCollectionNavigationProperties(entity);
              var complexProperties = typeMapper.GetComplexProperties(entity);
          
              if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())
              {
          #>
          Ответить
      • Не похоже
        Ответить

    Добавить комментарий