本文整理汇总了C#中Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromStatement方法的典型用法代码示例。如果您正苦于以下问题:C# CSharpCodeProvider.GenerateCodeFromStatement方法的具体用法?C# CSharpCodeProvider.GenerateCodeFromStatement怎么用?C# CSharpCodeProvider.GenerateCodeFromStatement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CSharp.CSharpCodeProvider
的用法示例。
在下文中一共展示了CSharpCodeProvider.GenerateCodeFromStatement方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateCode
public static string GenerateCode(CodeObject codeObj)
{
var provider = new CSharpCodeProvider();
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
{
if (codeObj is CodeCompileUnit)
{
provider.GenerateCodeFromCompileUnit(codeObj as CodeCompileUnit, writer, null);
}
else if (codeObj is CodeExpression)
{
provider.GenerateCodeFromExpression(codeObj as CodeExpression, writer, null);
}
else if (codeObj is CodeStatement)
{
provider.GenerateCodeFromStatement(codeObj as CodeStatement, writer, null);
}
else if (codeObj is CodeTypeMember)
{
provider.GenerateCodeFromMember(codeObj as CodeTypeMember, writer, null);
}
else if (codeObj is CodeNamespace)
{
provider.GenerateCodeFromNamespace(codeObj as CodeNamespace, writer, null);
}
else
{
throw new InvalidOperationException();
}
writer.Flush();
stream.Position = 0;
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
示例2: GenerateInitializeComponents
string GenerateInitializeComponents(CodeMemberMethod codeMethod, string indentation, string newline)
{
var writer = new StringWriter();
writer.NewLine = newline;
var options = new CodeGeneratorOptions();
options.IndentString = SD.EditorControlService.GlobalOptions.IndentationString;
var codeProvider = new CSharpCodeProvider();
foreach (CodeStatement statement in codeMethod.Statements) {
writer.Write(indentation);
// indentation isn't generated when calling GenerateCodeFromStatement
writer.Write(options.IndentString);
try {
codeProvider.GenerateCodeFromStatement(statement, writer, options);
} catch (Exception e) {
writer.WriteLine("// TODO: Error while generating statement : " + e.Message);
SD.Log.Error(e);
}
}
return writer.ToString();
}
示例3: CompileExpressions
public void CompileExpressions()
{
var csharp = new CSharpCodeProvider();
var w = new StreamWriter(new FileStream("compiled.cs", FileMode.Create));
var c = new CodeCommentStatement("This file is dynamically generated");
csharp.GenerateCodeFromStatement(c, w, null);
var namespc = new CodeNamespace("Melon.Reports");
namespc.Imports.Add(new CodeNamespaceImport("System"));
namespc.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
namespc.Imports.Add(new CodeNamespaceImport("Melon.Reports.Objects"));
var ExpressionCalculatorClass = new CodeTypeDeclaration("ExpressionCalculator")
{
IsClass = true,
TypeAttributes = TypeAttributes.Public
};
namespc.Types.Add(ExpressionCalculatorClass);
ExpressionCalculatorClass.BaseTypes.Add("Melon.Reports.AbstractCalculator");
ExpressionCalculatorClass.Members.Add(createConstructor());
foreach (var field in fields)
{
var f = new CodeMemberField(field.Type, field.Name) { Attributes = MemberAttributes.Public };
ExpressionCalculatorClass.Members.Add(f);
}
//especial variables
ExpressionCalculatorClass.Members.Add(new CodeMemberField(typeof (int), "GlobalRecordCount") {Attributes = MemberAttributes.Public});
ExpressionCalculatorClass.Members.Add(new CodeMemberField(typeof (int), "PageNumber") {Attributes = MemberAttributes.Public});
/*
* public object EvaluateVariableExpression(int hashcode){
* .....
* }
*/
var dynaCode = new StringBuilder("Object o = null; \n switch(i){\n");
foreach (var variable in variables.Values)
{
dynaCode.Append(Case(variable));
ExpressionCalculatorClass.Members.Add(createProperty(variable));
}
dynaCode.Append(" }\nreturn o");
ExpressionCalculatorClass.Members.Add(createMethod(dynaCode, "EvaluateVariableExpression"));
/* add expressions using the hash trick
*
* public virtual object EvaluateExpression(int i) {
* Object o = null;
* switch(i){
* case 13:
* o = (System.String)System.DateTime.Today.ToShortDateString();
* break;
* case ........
* ..
* }
* }
*/
dynaCode = new StringBuilder("Object o = null; \n switch(i){\n");
foreach (var ex in expressions)
{
dynaCode.Append(" case " + ex.GetHashCode() + ":\n" +
" o = " + ex.Content + ";\n" +
" break;\n");
}
dynaCode.Append(" }\n return o");
ExpressionCalculatorClass.Members.Add(createMethod2(dynaCode, "EvaluateExpression"));
var cu = new CodeCompileUnit();
cu.Namespaces.Add(namespc);
csharp.GenerateCodeFromNamespace(namespc, w, null);
w.Close();
var thisAsembly = Assembly.GetAssembly(GetType()).Location;
//compilation
var compparams = new CompilerParameters(new[] {"mscorlib.dll", thisAsembly}) {GenerateInMemory = true};
var ErrorMsg = "";
var cscompiler = csharp.CreateCompiler();
var compresult = cscompiler.CompileAssemblyFromDom(compparams, cu);
if (compresult == null || compresult.Errors.Count > 0)
{
if (compresult != null)
if (compresult.Errors.Count > 0)
{
//.........这里部分代码省略.........
示例4: GenerateCSharpTo
/// <summary>
/// Generates CSharp text from a statement and writes it to a text writer with the given options.
/// </summary>
/// <param name="statement">The statement to generate text from.</param>
/// <param name="writer">The text writer to write to.</param>
/// <param name="options">The generation options.</param>
public static void GenerateCSharpTo(this CodeStatement statement, TextWriter writer, CodeGeneratorOptions options)
{
using (var provider = new CSharpCodeProvider())
{
provider.GenerateCodeFromStatement(statement, writer, options);
}
}
示例5: GenerateCodeFromStatement
//used for anonymous delegates. No codedom for those. Could change to actual delegates. Not idea but like anonymouse delagates
private string GenerateCodeFromStatement(CodeStatement statement)
{
string code = string.Empty;
using (CSharpCodeProvider csc = new CSharpCodeProvider())
using (StringWriter sw = new StringWriter())
{
csc.GenerateCodeFromStatement(statement, sw, new CodeGeneratorOptions());
code = sw.ToString();
}
return code;
}