本文整理汇总了C#中RazorEngineHost类的典型用法代码示例。如果您正苦于以下问题:C# RazorEngineHost类的具体用法?C# RazorEngineHost怎么用?C# RazorEngineHost使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RazorEngineHost类属于命名空间,在下文中一共展示了RazorEngineHost类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BackgroundParser
public BackgroundParser(RazorEngineHost host, string fileName)
{
_main = new MainThreadState(fileName);
_bg = new BackgroundThread(_main, host, fileName);
_main.ResultsReady += (sender, args) => OnResultsReady(args);
}
示例2: CodeTreeWithUsings
public void CodeTreeWithUsings()
{
var syntaxTreeNode = new Mock<Span>(new SpanBuilder());
var language = new CSharpRazorCodeLanguage();
var host = new RazorEngineHost(language);
var codeBuilderContext = new CodeBuilderContext(
host,
"TestClass",
"TestNamespace",
"Foo.cs",
shouldGenerateLinePragmas: false,
errorSink: new ParserErrorSink());
codeBuilderContext.CodeTreeBuilder.AddUsingChunk("FakeNamespace1", syntaxTreeNode.Object);
codeBuilderContext.CodeTreeBuilder.AddUsingChunk("FakeNamespace2.SubNamespace", syntaxTreeNode.Object);
var codeBuilder = language.CreateCodeBuilder(codeBuilderContext);
// Act
var result = codeBuilder.Build();
BaselineWriter.WriteBaseline(
@"test\Microsoft.AspNet.Razor.Test\TestFiles\CodeGenerator\CS\Output\CSharpCodeBuilder.cs",
result.Code);
var expectedOutput = TestFile.Create("TestFiles/CodeGenerator/CS/Output/CSharpCodeBuilder.cs").ReadAllText();
// Assert
Assert.Equal(expectedOutput, result.Code);
}
示例3: Create
internal static CodeGeneratorContext Create(RazorEngineHost host, Func<CodeWriter> writerFactory, string className, string rootNamespace, string sourceFile, bool shouldGenerateLinePragmas)
{
CodeGeneratorContext context = new CodeGeneratorContext()
{
Host = host,
CodeWriterFactory = writerFactory,
SourceFile = shouldGenerateLinePragmas ? sourceFile : null,
CompileUnit = new CodeCompileUnit(),
Namespace = new CodeNamespace(rootNamespace),
GeneratedClass = new CodeTypeDeclaration(className)
{
IsClass = true
},
TargetMethod = new CodeMemberMethod()
{
Name = host.GeneratedClassContext.ExecuteMethodName,
Attributes = MemberAttributes.Override | MemberAttributes.Public
},
CodeMappings = new Dictionary<int, GeneratedCodeMapping>()
};
context.CompileUnit.Namespaces.Add(context.Namespace);
context.Namespace.Types.Add(context.GeneratedClass);
context.GeneratedClass.Members.Add(context.TargetMethod);
context.Namespace.Imports.AddRange(host.NamespaceImports
.Select(s => new CodeNamespaceImport(s))
.ToArray());
return context;
}
示例4: CalculatePadding
// internal for unit testing only, not intended to be used directly in code
internal static int CalculatePadding(RazorEngineHost host, Span target, int generatedStart)
{
if (host == null)
{
throw new ArgumentNullException("host");
}
if (target == null)
{
throw new ArgumentNullException("target");
}
int padding;
padding = CollectSpacesAndTabs(target, host.TabSize) - generatedStart;
// if we add generated text that is longer than the padding we wanted to insert we have no recourse and we have to skip padding
// example:
// Razor code at column zero: @somecode()
// Generated code will be:
// In design time: __o = somecode();
// In Run time: Write(somecode());
//
// In both cases the padding would have been 1 space to remote the space the @ symbol takes, which will be smaller than the 6 chars the hidden generated code takes.
if (padding < 0)
{
padding = 0;
}
return padding;
}
示例5: PadStatement
// Special case for statement padding to account for brace positioning in the editor.
public static string PadStatement(RazorEngineHost host, string code, Span target, ref int startGeneratedCode, out int paddingCharCount)
{
if (host == null)
{
throw new ArgumentNullException("host");
}
if (target == null)
{
throw new ArgumentNullException("target");
}
// We are passing 0 rather than startgeneratedcode intentionally (keeping v2 behavior).
int padding = CalculatePadding(host, target, 0);
// We treat statement padding specially so for brace positioning, so that in the following example:
// @if (foo > 0)
// {
// }
//
// the braces shows up under the @ rather than under the if.
if (host.DesignTimeMode &&
padding > 0 &&
target.Previous.Kind == SpanKind.Transition && // target.Previous is guaranteed to be none null if you got any padding.
String.Equals(target.Previous.Content, SyntaxConstants.TransitionString))
{
padding--;
startGeneratedCode--;
}
string generatedCode = PadInternal(host, code, padding, out paddingCharCount);
return generatedCode;
}
示例6: RazorChunkGenerator
public RazorChunkGenerator(
string className,
string rootNamespaceName,
string sourceFileName,
RazorEngineHost host)
{
if (rootNamespaceName == null)
{
throw new ArgumentNullException(nameof(rootNamespaceName));
}
if (host == null)
{
throw new ArgumentNullException(nameof(host));
}
if (string.IsNullOrEmpty(className))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, nameof(className));
}
ClassName = className;
RootNamespaceName = rootNamespaceName;
SourceFileName = sourceFileName;
GenerateLinePragmas = string.IsNullOrEmpty(SourceFileName) ? false : true;
Host = host;
}
示例7: ConstructorInitializesHost
public void ConstructorInitializesHost() {
// Arrange
RazorEngineHost host = new RazorEngineHost(new CSharpRazorCodeLanguage());
// Act
RazorTemplateEngine engine = new RazorTemplateEngine(host);
// Assert
Assert.AreSame(host, engine.Host);
}
示例8: Create
public static CodeGeneratorContext Create(RazorEngineHost host, string className, string rootNamespace, string sourceFile, bool shouldGenerateLinePragmas)
{
return new CodeGeneratorContext()
{
CodeTreeBuilder = new CodeTreeBuilder(),
Host = host,
SourceFile = shouldGenerateLinePragmas ? sourceFile : null,
RootNamespace = rootNamespace,
ClassName = className
};
}
示例9: CodeBuilderContext
// Internal for testing.
internal CodeBuilderContext(RazorEngineHost host,
string className,
string rootNamespace,
string sourceFile,
bool shouldGenerateLinePragmas,
ParserErrorSink errorSink)
: base(host, className, rootNamespace, sourceFile, shouldGenerateLinePragmas)
{
ErrorSink = errorSink;
ExpressionRenderingMode = ExpressionRenderingMode.WriteToOutput;
}
示例10: ConstructorWithCodeLanguageSetsPropertiesAppropriately
public void ConstructorWithCodeLanguageSetsPropertiesAppropriately() {
// Arrange
RazorCodeLanguage language = new CSharpRazorCodeLanguage();
// Act
RazorEngineHost host = new RazorEngineHost(language);
// Assert
VerifyCommonDefaults(host);
Assert.AreSame(language, host.CodeLanguage);
Assert.IsInstanceOfType(host.CreateMarkupParser(), typeof(HtmlMarkupParser));
}
示例11: CodeGeneratorContext
public CodeGeneratorContext(RazorEngineHost host,
string className,
string rootNamespace,
string sourceFile,
bool shouldGenerateLinePragmas)
{
CodeTreeBuilder = new CodeTreeBuilder();
Host = host;
SourceFile = shouldGenerateLinePragmas ? sourceFile : null;
RootNamespace = rootNamespace;
ClassName = className;
}
示例12: SetBaseTypeFromHost
private void SetBaseTypeFromHost(RazorEngineHost mvcHost)
{
if (!mvcHost.DefaultBaseTemplateType.IsGenericType)
{
SetBaseType(mvcHost.DefaultBaseTemplateType.FullName);
}
else
{
var modelTypeName = CompilerServicesUtility.ResolveCSharpTypeName(mvcHost.DefaultModelType);
SetBaseType(mvcHost.DefaultBaseClass + "<" + modelTypeName + ">");
}
}
示例13: ConstructorWithCodeLanguageSetsPropertiesAppropriately
public void ConstructorWithCodeLanguageSetsPropertiesAppropriately()
{
// Arrange
var language = new CSharpRazorCodeLanguage();
// Act
var host = new RazorEngineHost(language);
// Assert
VerifyCommonDefaults(host);
Assert.Same(language, host.CodeLanguage);
Assert.IsType<HtmlMarkupParser>(host.CreateMarkupParser());
}
示例14: ConstructorWithCodeLanguageAndMarkupParserSetsPropertiesAppropriately
public void ConstructorWithCodeLanguageAndMarkupParserSetsPropertiesAppropriately() {
// Arrange
RazorCodeLanguage language = new CSharpRazorCodeLanguage();
MarkupParser expected = new HtmlMarkupParser();
// Act
RazorEngineHost host = new RazorEngineHost(language, () => expected);
// Assert
VerifyCommonDefaults(host);
Assert.AreSame(language, host.CodeLanguage);
Assert.AreSame(expected, host.CreateMarkupParser());
}
示例15: PaddingCharCount
// there is some duplicity of code here, but its very simple and since this is a host path, I'd rather not create another class to encapsulate the data.
public static int PaddingCharCount(RazorEngineHost host, Span target, int generatedStart)
{
int padding = CalculatePadding(host, target, generatedStart);
if (host.DesignTimeMode && host.IsIndentingWithTabs)
{
int spaces;
int tabs = Math.DivRem(padding, host.TabSize, out spaces);
return tabs + spaces;
}
else
{
return padding;
}
}