Text patterns for generating strings at runtime (e.g. Razor or T4)

Is there any tool on the Internet that can be used to generate strings from a template, I'm looking for something similar to Razor.

Lines must be generated at run time and are not dependent on Visual Studio (e.g. T4). And the structure should work in Silverlight.

RazorEngine is a structure that responds to requests but does not work in Silverlight.

Thanks in advance.

+6
source share
1 answer

I hope I understand what you asked for, but I would say that you can get T4 to work in SL as well. T4 can be asked to generate what are sometimes called runtime patterns. I defined my template (very simple) and added it to my Silverlight project.

<# for (var iter = 0; iter < 10; ++iter) { #> This is just a test: #<#=iter#> <# } #> 

Usually it generates this output:

 This is just a test: #0 This is just a test: #1 This is just a test: #2 This is just a test: #3 This is just a test: #4 This is just a test: #5 This is just a test: #6 This is just a test: #7 This is just a test: #8 This is just a test: #9 

But in this case, I like to generate code that generates this output, i.e. runtime pattern. To do this, I will switch my own tool: TextTemplatingFilePreprocessor

Now the template generates code that generates this output. If you avoid hostpecific = true, you are not getting Visual Studio dependencies. By extending a partial class with member variables and referencing them from a template file, you can change the behavior of the template at run time.

The problem with Silverlight is that there are no classes in Silverlight: System.CodeDom.Compiler.CompilerError and System.CodeDom.Compiler.CompilerErrorCollection.

I worked on this by creating my own classes for this (just for this purpose):

 namespace System.CodeDom.Compiler { public class CompilerError { public string ErrorText; public bool IsWarning; } public class CompilerErrorCollection : List<CompilerError> { } } 

Now my template compiles, and I just like this from my Silverlight application to create the output:

 var runtimeTemplate = new MyRuntimeTemplate(); string output = runtimeTemplate.TransformText(); 
+5
source

Source: https://habr.com/ru/post/899006/


All Articles