当前位置: 首页>>代码示例>>C#>>正文


C# Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromExpression方法代码示例

本文整理汇总了C#中Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromExpression方法的典型用法代码示例。如果您正苦于以下问题:C# Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromExpression方法的具体用法?C# Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromExpression怎么用?C# Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromExpression使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.CSharp.CSharpCodeProvider的用法示例。


在下文中一共展示了Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromExpression方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GenerateCode

 public void GenerateCode(CodeDomGenerator.Language language)
 {
     switch (language)
     {
         case CodeDomGenerator.Language.CSharp:
             using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
             {
                 System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                 using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                 {
                     tw.Write("new ");
                     provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(TypeReference.ArrayElementType), tw, opts);
                     tw.Write("[");
                     for (int i = 0; i < Dimensions.Count; i++)
                     {
                         if (i > 0)
                             tw.Write(",");
                         provider.GenerateCodeFromExpression(Dimensions[i], tw, opts);
                     }
                     tw.Write("]");
                     Value = tw.InnerWriter.ToString();
                 }
             }
             break;
         case CodeDomGenerator.Language.VB:
             using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
             {
                 System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                 using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                 {
                     tw.Write("(");
                     for (int i = 0; i < Dimensions.Count; i++)
                     {
                         if (i > 0)
                             tw.Write(",");
                         tw.Write("(");
                         provider.GenerateCodeFromExpression(Dimensions[i], tw, opts);
                         tw.Write(")-1");
                     }
                     tw.Write(") as ");
                     provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(TypeReference), tw, opts);
                     Value = tw.InnerWriter.ToString();
                 }
             }
             break;
         default:
             throw new NotImplementedException(language.ToString());
     }
 }
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:49,代码来源:CodeMultidimensionalArrayCreateExpression.cs

示例2: GenerateCode

 public void GenerateCode(CodeDomGenerator.Language language)
 {
     switch (language)
     {
         case CodeDomGenerator.Language.CSharp:
             using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
             {
                 System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                 using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                 {
                     tw.Write("!");
                     provider.GenerateCodeFromExpression(Expression, tw, opts);
                     Value = tw.InnerWriter.ToString();
                 }
             }
             break;
         case CodeDomGenerator.Language.VB:
             using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
             {
                 System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                 using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                 {
                     tw.Write("Not ");
                     provider.GenerateCodeFromExpression(Expression, tw, opts);
                     Value = tw.InnerWriter.ToString();
                 }
             }
             break;
         default:
             throw new NotImplementedException(language.ToString());
     }
 }
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:32,代码来源:CodeNotExpression.cs

示例3: GenerateCode

        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
                case CodeDomGenerator.Language.CSharp:
                    using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {

                            tw.WriteLine("do");
                            tw.WriteLine("{");
                            tw.Indent++;
                            if (Statements != null)
                                foreach (CodeStatement statement in Statements)
                                {
                                    provider.GenerateCodeFromStatement(statement, tw, opts);
                                }
                            tw.Indent--;
                            tw.Write("} while (");
                            if (TestExpression != null)
                                provider.GenerateCodeFromExpression(TestExpression, tw, opts);
                            tw.WriteLine(");");

                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                case CodeDomGenerator.Language.VB:
                    using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {
                            tw.WriteLine("Do");
                            tw.Indent++;
                            if (Statements != null)
                                foreach (CodeStatement statement in Statements)
                                {
                                    provider.GenerateCodeFromStatement(statement, tw, opts);
                                }
                            tw.Indent--;
                            tw.Write("Loop Until ");
                            if (TestExpression != null)
                                provider.GenerateCodeFromExpression(TestExpression, tw, opts);
                            tw.WriteLine();

                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                default:
                    throw new NotImplementedException(language.ToString());
            }
        }
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:56,代码来源:CodeWhileStatement.cs

示例4: GetPrimitiveValueLiteral

        /// <summary>
        /// Gets the primitive value literal.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>System.String.</returns>
        public static string GetPrimitiveValueLiteral(object value)
        {
            using (var writer = new StringWriter(CultureInfo.InvariantCulture))
            using (var provider = new Microsoft.CSharp.CSharpCodeProvider())
            {
                provider.GenerateCodeFromExpression(new CodePrimitiveExpression(value), writer, null);

                return writer.ToString();
            }
        }
开发者ID:mparsin,项目名称:Elements,代码行数:15,代码来源:StringExtensions.cs

示例5: GenerateCS

 private void GenerateCS()
 {
     using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
     {
         CodeGeneratorOptions opts = new CodeGeneratorOptions();
         StringBuilder sb = new StringBuilder();
         using (IndentedTextWriter tw = new IndentedTextWriter(new StringWriter(sb), opts.IndentString))
         {
             provider.GenerateCodeFromMember(Field, tw, opts);
             var type = new StringWriter();
             provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(Field.Type), type, opts);
             sb.Replace(" " + type.GetStringBuilder(), " readonly " + type.GetStringBuilder());
         }
         Text = sb.ToString();
     }
 }
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:16,代码来源:CodeReadonlyField.cs

示例6: GenerateCode

 public void GenerateCode(CodeDomGenerator.Language language)
 {
     switch (language)
     {
         case CodeDomGenerator.Language.CSharp:
             using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
             {
                 System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                 using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                 {
                     provider.GenerateCodeFromExpression(Parameter, tw, opts);
                     Value = tw.InnerWriter.ToString();
                 }
             }
             break;
         case CodeDomGenerator.Language.VB:
             using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
             {
                 System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                 StringBuilder sb = new StringBuilder();
                 using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(sb), opts.IndentString))
                 {
                     provider.GenerateCodeFromExpression(Parameter, tw, opts);
                     switch (Option)
                     {
                         case ParamOption.Optional:
                             sb.Replace("ByVal " + Parameter.Name, "Optional ByVal " + Parameter.Name);
                             break;
                         case ParamOption.ParamArray:
                             sb.Replace("ByVal " + Parameter.Name, "ByVal ParamArray " + Parameter.Name);
                             break;
                         default:
                             throw new NotSupportedException(Option.ToString());
                     }
                     Value = tw.InnerWriter.ToString();
                 }
             }
             break;
         default:
             throw new NotImplementedException(language.ToString());
     }
 }
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:42,代码来源:CodeParamExpression.cs

示例7: GenerateCS

        private void GenerateCS()
        {
            using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
            {
                CodeGeneratorOptions opts = new CodeGeneratorOptions();
                StringWriter sw = new StringWriter();
                provider.GenerateCodeFromMember(_property, sw, opts);
                StringReader sr = new StringReader(sw.GetStringBuilder().ToString());
                string line = sr.ReadLine();
                while (string.IsNullOrEmpty(line))
                    line = sr.ReadLine();
                int idx = line.IndexOf(" " + _property.Name);

                StringBuilder sb = new StringBuilder();
                sb.Append(" this[");
                foreach (CodeParameterDeclarationExpression parameter in Parameters)
                {
                    provider.GenerateCodeFromExpression(parameter, new StringWriter(sb), opts);
                    sb.Append(", ");
                }
                sb.Length -= 2;
                sb.Append("]");
                Text = sw.GetStringBuilder().Replace(line, line.Substring(0, idx) + sb.ToString() + line.Substring(idx+_property.Name.Length+1)).ToString();
            }
        }
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:25,代码来源:CodeDefaultProperty.cs

示例8: GenerateCode

        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
                case CodeDomGenerator.Language.CSharp:
                    using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {

                            tw.Write("foreach (");
                            provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(IterationItemType), tw, opts);
                            tw.Write(" {0} in ", provider.CreateValidIdentifier(IterationItemName));
                            provider.GenerateCodeFromExpression(IterExpression, tw, opts);
                            tw.WriteLine(")");
                            tw.WriteLine("{");
                            tw.Indent++;
                            if (Statements != null)
                                foreach (CodeStatement statement in Statements)
                                {
                                    provider.GenerateCodeFromStatement(statement, tw, opts);
                                }
                            tw.Indent--;
                            tw.WriteLine("}");
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                case CodeDomGenerator.Language.VB:
                    using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {
                            tw.Write("For Each {0} As ", provider.CreateValidIdentifier(IterationItemName));
                            provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(IterationItemType), tw, opts);
                            tw.Write(" in ");
                            provider.GenerateCodeFromExpression(IterExpression, tw, opts);
                            tw.WriteLine();
                            tw.Indent++;
                            if (Statements != null)
                                foreach (CodeStatement statement in Statements)
                                {
                                    provider.GenerateCodeFromStatement(statement, tw, opts);
                                }
                            tw.Indent--;
                            tw.WriteLine("Next");
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                default:
                    throw new NotImplementedException(language.ToString());
            }
        }
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:56,代码来源:CodeForeachStatement.cs

示例9: ToCSharpString

        /// <summary>
        /// Returns a valid C# representation of a typed argument that can be 
        /// used in a custom attribute for code generation.
        /// </summary>
        public string ToCSharpString(CustomAttributeTypedArgument argument)
        {
            using (var provider = new Microsoft.CSharp.CSharpCodeProvider())
            using (var writer = new System.IO.StringWriter(CultureInfo.InvariantCulture))
            {
                if (argument.Value == null)
                {
                    provider.GenerateCodeFromExpression(new System.CodeDom.CodePrimitiveExpression(null), writer, new System.CodeDom.Compiler.CodeGeneratorOptions());
                    return writer.ToString();
                }

                if (argument.ArgumentType == typeof(string))
                    return "\"" + argument.Value + "\"";

                if (argument.ArgumentType.IsEnum)
                    return argument.ArgumentType.Name + "." + Enum.GetName(argument.ArgumentType, argument.Value);

                if (argument.ArgumentType == typeof(Type))
                    provider.GenerateCodeFromExpression(new System.CodeDom.CodeTypeOfExpression(((Type)argument.Value).Name), writer, new System.CodeDom.Compiler.CodeGeneratorOptions());
                else
                    provider.GenerateCodeFromExpression(new System.CodeDom.CodePrimitiveExpression(argument.Value), writer, new System.CodeDom.Compiler.CodeGeneratorOptions());

                return writer.ToString();
            }
        }
开发者ID:w0rd-driven,项目名称:funq,代码行数:29,代码来源:CodeGeneration.cs

示例10: ToLiteral

 static string ToLiteral(string input)
 {
     var writer = new System.IO.StringWriter();
     Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
     provider.GenerateCodeFromExpression(new System.CodeDom.CodePrimitiveExpression(input), writer, null);
     return writer.GetStringBuilder().ToString();
 }
开发者ID:JohnThomson,项目名称:libpalaso,代码行数:7,代码来源:LinuxEmailProvider.cs

示例11: GenerateCode

        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
                case CodeDomGenerator.Language.CSharp:
                    using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {
                            if (_params.Count != 1 || _params[0].Type != null)
                                tw.Write("(");

                            for (int i = 0; i < _params.Count; i++)
                            {
                                if (i > 0)
                                    tw.Write(", ");

                                if (_params[i].Type != null)
                                {
                                    provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(_params[i].Type), tw, opts);
                                    tw.Write(" ");
                                }

                                tw.Write(_params[i].Name);
                            }

                            if (_params.Count != 1 || _params[0].Type != null)
                                tw.Write(")");

                            tw.Write(" => ");
                            provider.GenerateCodeFromExpression(Expression, tw, opts);
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                case CodeDomGenerator.Language.VB:
                    using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {
                            tw.Write("Function(");

                            for (int i = 0; i < _params.Count; i++)
                            {
                                if (i > 0)
                                    tw.Write(", ");

                                tw.Write(_params[i].Name);

                                if (_params[i].Type != null)
                                {
                                    tw.Write(" As ");
                                    provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(_params[i].Type), tw, opts);
                                }
                            }

                            tw.Write(") ");
                            provider.GenerateCodeFromExpression(Expression, tw, opts);
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                default:
                    throw new NotImplementedException(language.ToString());
            }
        }
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:68,代码来源:CodeLambdaExpression.cs

示例12: GenerateCode

        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
                case CodeDomGenerator.Language.CSharp:
                    using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {

                            tw.Write("switch (");
                            if (SwitchExpression != null)
                                provider.GenerateCodeFromExpression(SwitchExpression, tw, opts);
                            tw.WriteLine(")");
                            tw.WriteLine("{");
                            tw.Indent++;
                            foreach (CaseBlock c in m_cases)
                            {
                                if (c.Statements != null)
                                {
                                    if (c.CaseExpression != null)
                                    {
                                        tw.Write("case ");
                                        provider.GenerateCodeFromExpression(c.CaseExpression, tw, opts);
                                        tw.WriteLine(":");
                                    }
                                    else
                                    {
                                        tw.WriteLine("default:");
                                    }

                                    tw.Indent++;
                                    foreach (CodeStatement stmt in c.Statements)
                                    {
                                        provider.GenerateCodeFromStatement(stmt, tw, opts);
                                    }
                                    tw.Indent--;
                                }
                                //else
                                //{
                                //    tw.Indent++;
                                //    tw.WriteLine("break;");
                                //    tw.Indent--;
                                //}
                            }
                            tw.Indent--;
                            tw.WriteLine("}");
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                case CodeDomGenerator.Language.VB:
                    using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {
                            tw.Write("Select Case ");
                            if (SwitchExpression != null)
                                provider.GenerateCodeFromExpression(SwitchExpression, tw, opts);
                            tw.WriteLine();
                            tw.Indent++;
                            foreach (CaseBlock c in m_cases)
                            {
                                if (c.Statements != null)
                                {
                                    if (c.CaseExpression != null)
                                    {
                                        tw.Write("Case ");
                                        provider.GenerateCodeFromExpression(c.CaseExpression, tw, opts);
                                        tw.WriteLine();
                                    }
                                    else
                                    {
                                        tw.WriteLine("Case Else");
                                    }

                                    tw.Indent++;
                                    for (int i = 0; i < c.Statements.Length; i++)
                                    {
                                        CodeStatement stmt = c.Statements[i];
                                        if (!(stmt is CodeExitSelectStatement) || i < c.Statements.Length-1)
                                            provider.GenerateCodeFromStatement(stmt, tw, opts);
                                    }
                                    tw.Indent--;
                                }
                                //else if (i < m_cases.Count - 1)
                                //{
                                //    tw.Indent++;
                                //    tw.WriteLine("Exit Select");
                                //    tw.Indent--;
                                //}
                            }
                            tw.Indent--;
                            tw.WriteLine("End Select");
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
//.........这里部分代码省略.........
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:101,代码来源:CodeSwitchStatement.cs


注:本文中的Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromExpression方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。