本文整理汇总了C#中System.CodeDom.Compiler.IndentedTextWriter.Close方法的典型用法代码示例。如果您正苦于以下问题:C# IndentedTextWriter.Close方法的具体用法?C# IndentedTextWriter.Close怎么用?C# IndentedTextWriter.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.CodeDom.Compiler.IndentedTextWriter
的用法示例。
在下文中一共展示了IndentedTextWriter.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public void Run()
{
var compileUnit = new CodeCompileUnit();
var myNamespace = new CodeNamespace("MyNamespace");
myNamespace.Imports.Add(new CodeNamespaceImport("System"));
var myClass = new CodeTypeDeclaration("MyClass");
var start = new CodeEntryPointMethod();
var cs1 = new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression("Console"),
"WriteLine", new CodePrimitiveExpression("Hello World!"));
compileUnit.Namespaces.Add(myNamespace);
myNamespace.Types.Add(myClass);
myClass.Members.Add(start);
start.Statements.Add(cs1);
var provider = new CSharpCodeProvider();
using (var sw = new StreamWriter("HelloWorld.cs", false))
{
var tw = new IndentedTextWriter(sw, " ");
provider.GenerateCodeFromCompileUnit(compileUnit, tw,
new CodeGeneratorOptions());
tw.Close();
}
}
示例2: ParseToCode
public GeneratorResults ParseToCode(string TemplateCode, string defaultnamespace, string defaultclassname, string baseClassFullName)
{
GeneratorResults razorResults;
var host = new RazorEngineHost(new CSharpRazorCodeLanguage());
host.DefaultBaseClass = baseClassFullName;//typeof(BulaqTemplateForRazorBase).FullName;
host.DefaultNamespace = defaultnamespace;
host.DefaultClassName = defaultclassname;
host.NamespaceImports.Add("System");
host.NamespaceImports.Add("BulaqCMS.Models");
host.GeneratedClassContext = new GeneratedClassContext("Execute", "Write", "WriteLiteral");
var engine = new RazorTemplateEngine(host);
using (var reader = new StringReader(TemplateCode))
{
razorResults = engine.GenerateCode(reader);
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CodeGeneratorOptions options = new CodeGeneratorOptions();
options.BracingStyle = "C";
using (StringWriter writer = new StringWriter())
{
IndentedTextWriter indentwriter = new IndentedTextWriter(writer, " ");
codeProvider.GenerateCodeFromCompileUnit(razorResults.GeneratedCode, indentwriter, options);
indentwriter.Flush();
indentwriter.Close();
LastGeneratedCode = writer.GetStringBuilder().ToString();
string codePath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\code.cs";
File.WriteAllText(codePath, LastGeneratedCode, Encoding.UTF8);
}
}
return razorResults;
}
示例3: WriteMigrationFile
public void WriteMigrationFile()
{
// check if migration context base class exists
string context = String.Format("{0}\\{1}.cs", Globals.Settings.Paths.Migration,
Globals.Settings.Names.MigrationContext);
if (!File.Exists(context))
{
WriteMigrationContextFile();
}
string migrationClass = String.Format(migrationName, Globals.Settings.Names.Database);
string path = String.Format("{0}\\{1}.cs", Globals.Settings.Paths.Migration, Migration.ClassFile);
var template = Activator.CreateInstance<DatabaseMigrationTemplate>();
var session = new TextTemplatingSession();
// add variables
session["migrationClass"] = Migration.ClassFile;
session["namespce"] = Globals.Settings.Namespaces.Migration;
session["databaseName"] = Globals.Settings.Names.Database;
session["connection"] = Globals.BasicConnectionString;
template.Session = session;
template.Initialize();
// generate the source file
string source = template.TransformText();
// Create a StreamWriter to the output file.
using (StreamWriter sw = new StreamWriter(path, false))
{
IndentedTextWriter writer = new IndentedTextWriter(sw);
writer.Write(source);
writer.Close();
}
}
示例4: Main
static void Main(string[] args)
{
CodeCompileUnit compileUnit = new CodeCompileUnit();
CodeNamespace myNamespace = new CodeNamespace("MyNamespace");
myNamespace.Imports.Add(new CodeNamespaceImport("System"));
CodeTypeDeclaration myClass = new CodeTypeDeclaration("MyClass");
CodeEntryPointMethod start = new CodeEntryPointMethod();
CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("Console"), "WriteLine", new CodePrimitiveExpression("Hello World"));
compileUnit.Namespaces.Add(myNamespace);
myNamespace.Types.Add(myClass);
myClass.Members.Add(start);
start.Statements.Add(cs1);
CSharpCodeProvider provider = new CSharpCodeProvider();
using(StreamWriter sw = new StreamWriter("HelloWorld.cs", false))
{
IndentedTextWriter tw = new IndentedTextWriter(sw, " ");
provider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions());
tw.Close();
}
Console.WriteLine("HelloWorld.cs generated in .../bin/Debug or .../bin/Release project folders.");
Console.Write("Press a key to exit");
Console.ReadKey();
}
示例5: CreateOutput
public override string CreateOutput()
{
RemoveComments = Generators.Any(p => !p.AlwaysRegenerate);
Namespace = new CodeNamespace(NamespaceName);
Unit = new CodeCompileUnit();
Unit.Namespaces.Add(Namespace);
foreach (var codeGenerator in Generators.Where(p=>p.IsValid()))
{
// UnityEngine.Debug.Log(codeGenerator.GetType().Name + " is generating");
codeGenerator.Initialize(this);
}
var provider = new CSharpCodeProvider();
var sb = new StringBuilder();
var tw1 = new IndentedTextWriter(new StringWriter(sb), " ");
provider.GenerateCodeFromCompileUnit(Unit, tw1, new CodeGeneratorOptions());
tw1.Close();
if (RemoveComments)
{
var removedLines = sb.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None).Skip(10).ToArray();
return string.Join(Environment.NewLine, removedLines);
}
return sb.ToString();
}
示例6: GenerateCSharpCode
public static String GenerateCSharpCode(CodeCompileUnit compileunit)
{
// Generate the code with the C# code provider.
CSharpCodeProvider provider = new CSharpCodeProvider();
// Build the output file name.
String sourceFile;
if (provider.FileExtension[0] == '.')
{
sourceFile = "HelloWorld" + provider.FileExtension;
}
else
{
sourceFile = "HelloWorld." + provider.FileExtension;
}
// Create a TextWriter to a StreamWriter to the output file.
IndentedTextWriter tw = new IndentedTextWriter(
new StreamWriter(sourceFile, false), " ");
// Generate source code using the code provider.
provider.GenerateCodeFromCompileUnit(compileunit, tw,
new CodeGeneratorOptions());
// Close the output file.
tw.Close();
return sourceFile;
}
示例7: GenerateSource
internal string GenerateSource(CodeDomProvider codeProvider)
{
StringBuilder sb = new StringBuilder();
TextWriter tWriter = new IndentedTextWriter(new StringWriter(sb));
CodeCompileUnit ccu = GenerateCode();
codeProvider.GenerateCodeFromCompileUnit(ccu, tWriter, new CodeGeneratorOptions());
codeProvider.Dispose();
tWriter.Close();
return sb.ToString();
}
示例8: Main
public static void Main()
{
CSharpCodeProvider cs = new CSharpCodeProvider();
CodeGenerator cg = new CodeGenerator();
using (StreamWriter sw=new StreamWriter("HelloWorld.cs",false))
{
IndentedTextWriter tw = new IndentedTextWriter(sw, " ");
cs.GenerateCodeFromCompileUnit(cg.GetCompierUnit(), tw, new CodeGeneratorOptions());
tw.Close();
}
}
示例9: GenerateFile
private void GenerateFile()
{
CSharpCodeProvider provider = new CSharpCodeProvider();
using (StreamWriter sw = new StreamWriter("HelloWorld.cs", false))
{
IndentedTextWriter tw = new IndentedTextWriter(sw, " ");
provider.GenerateCodeFromCompileUnit(GenerateHelloWorldCode(), tw, new CodeGeneratorOptions());
tw.Close();
}
}
示例10: Main
static void Main(string[] args)
{
CodeCompileUnit ccu = HelloWorldCodeDOM();
CSharpCodeProvider provider = new CSharpCodeProvider();
using(StreamWriter sw = new StreamWriter("HelloWorld.cs", false))
{
IndentedTextWriter tw = new IndentedTextWriter(sw, " ");
provider.GenerateCodeFromCompileUnit(ccu, tw, new CodeGeneratorOptions());
tw.Close();
}
}
示例11: GenerateCodeUsingCodeDOM
private void GenerateCodeUsingCodeDOM(CodeCompileUnit codeCompileUnit, string outputFile)
{
var options = new CodeGeneratorOptions
{
BracingStyle = "JS"
};
var codeDomProvider = new TypescriptCodeProvider();
using (var textWriter = new IndentedTextWriter(new StreamWriter(outputFile)))
{
codeDomProvider.GenerateCodeFromCompileUnit(codeCompileUnit, textWriter, options);
textWriter.Close();
}
}
示例12: GenerateClass
public void GenerateClass()
{
IndentedTextWriter writer = new IndentedTextWriter(new StreamWriter(this.fileName));
Console.WriteLine("Generating " + this.fileName);
WriteFileHeader(writer);
foreach (Stanza stanza in SyntaxInfo.Instance)
{
stanza.Generate(writer, this.className, isStatic);
}
WriteFileTrailer(writer);
writer.Close();
}
示例13: BuildAssembly
public Assembly BuildAssembly()
{
GetReferencedTypes().ForEach(AddAssemblyReference);
CSharpCodeProvider provider =
new CSharpCodeProvider();
ICodeCompiler compiler = provider.CreateCompiler();
CompilerResults results =
compiler.CompileAssemblyFromDom(CompilerParameters, this);
if (results.Errors.HasErrors)
{
StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
foreach (CompilerError error in results.Errors)
{
errors.AppendFormat("Line {0},{1}\t: {2}\n",
error.Line, error.Column, error.ErrorText);
}
var buffer = new StringBuilder();
using(var writer = new StringWriter(buffer))
{
using (var textWriter = new IndentedTextWriter(writer))
{
provider.GenerateCodeFromCompileUnit(this, textWriter, new CodeGeneratorOptions());
textWriter.Close();
}
}
var source = new StringBuilder();
var lines = buffer.ToString().Split(new string[] {Environment.NewLine}, StringSplitOptions.None);
for(int i = 0; i < lines.Length; i++)
{
source.AppendLine(i + lines[i]);
}
throw new Exception(
errors.ToString()
+ Environment.NewLine
+ Environment.NewLine
+ "CODE:"
+ Environment.NewLine
+ source);
}
return results.CompiledAssembly;
}
示例14: Main
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
CSharpCodeProvider provider = new CSharpCodeProvider();
using (StreamWriter sw = new StreamWriter("HelloWorld.cs", false))
{
IndentedTextWriter tw = new IndentedTextWriter(sw, " ");
provider.GenerateCodeFromCompileUnit(GetCode(), tw, new CodeGeneratorOptions());
tw.Close();
}
Func<int, int, int> addFunc = (x, y) => x + y;
Console.WriteLine(addFunc(2, 3));
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
示例15: Generate
public void Generate(CodeCompileUnit targetUnit)
{
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CodeGeneratorOptions options = new CodeGeneratorOptions();
options.BracingStyle = "C";
options.IndentString = " ";
options.BlankLinesBetweenMembers = true;
if (Directory.Exists(destinationPath) && ConfigurationApp.GetProperty("IsGeneratedSolutionDirectoryPurged") != "true")
{
Directory.Delete(destinationPath, true);
ConfigurationApp.SaveProperty("IsGeneratedSolutionDirectoryPurged", "true");
}
Directory.CreateDirectory(destinationPath);
// Build the output file name.
foreach (CodeNamespace namespaceUnit in targetUnit.Namespaces)
{
var namespaceName = namespaceUnit.Name;
string sourceFile;
if (provider.FileExtension[0] == '.')
{
sourceFile = this.PathFromNamespace(destinationPath, namespaceName) + "/" + namespaceUnit.Types[0].Name + provider.FileExtension;
}
else
{
sourceFile = this.PathFromNamespace(destinationPath, namespaceName) + "/" + namespaceUnit.Types[0].Name + "." + provider.FileExtension;
}
using (StreamWriter sw = new StreamWriter(sourceFile, false))
{
IndentedTextWriter tw = new IndentedTextWriter(sw, " ");
provider.GenerateCodeFromNamespace(namespaceUnit, tw, options);
tw.Close();
}
Console.WriteLine(sourceFile + " has been generated");
}
}