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


C# CSharpCodeProvider.GenerateCodeFromExpression方法代码示例

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


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

示例1: PrimitiveLiteral

 /// <summary>
 /// Returns the C# literal representation of a given primitive expression.
 /// (Useful for escaping strings)
 /// </summary>
 private static string PrimitiveLiteral(this string input)
 {
     var writer = new StringWriter();
     var provider = new CSharpCodeProvider();
     provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, null);
     return writer.ToString();
 }
开发者ID:ChrisFulstow,项目名称:CSharpCodeGen,代码行数:11,代码来源:CodeGenerator.cs

示例2: ToLiteral

 /// <summary>
 /// Converts a string to a C sharp string literal.
 /// </summary>
 public static string ToLiteral(this string input)
 {
     var writer = new StringWriter();
     CSharpCodeProvider provider = new CSharpCodeProvider();
     provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, null);
     return writer.GetStringBuilder().ToString();
 }
开发者ID:nexaddo,项目名称:Unity-Version-Control,代码行数:10,代码来源:StringHelpers.cs

示例3: ToString

        public static string ToString(object value)
        {
            var text = new StringBuilder();

            using (var provider = new CSharpCodeProvider())
            {
                using (var writer = new StringWriter(text, CultureInfo.InvariantCulture))
                {
                    provider.GenerateCodeFromExpression(new CodePrimitiveExpression(value), writer, DefaultCodeGeneratorOptions);
                }
            }

            return text.ToString();
        }
开发者ID:jmangelo,项目名称:Inthros,代码行数:14,代码来源:CSharpConvert.cs

示例4: 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();
                }
            }
        }
开发者ID:robocoder,项目名称:aphid,代码行数:40,代码来源:CSharpHelper.cs

示例5: TypeToString

        // http://stackoverflow.com/a/1363212/1060807
        public static string TypeToString(Type type)
        {
            if (type == null) throw new ArgumentNullException("type");

            var sb = new StringBuilder();
            using (var sw = new StringWriter(sb))
            {
                var expr = new CodeTypeReferenceExpression(type);
                var prov = new CSharpCodeProvider();

                prov.GenerateCodeFromExpression(expr, sw, new CodeGeneratorOptions());
            }
            return sb.ToString();
        }
开发者ID:NodeRT,项目名称:NodeRT,代码行数:15,代码来源:Reflector.cs

示例6: FormatArgument

 private static string FormatArgument(object arg)
 {
     using(StringWriter sw = new StringWriter())
     {
         CSharpCodeProvider provider = new CSharpCodeProvider();
         provider.GenerateCodeFromExpression(new CodePrimitiveExpression(arg), sw, null);
         return sw.ToString();
     }
 }
开发者ID:ChrisPelatari,项目名称:XunitForms,代码行数:9,代码来源:EventAction.cs

示例7: GenerateCSharpTo

 /// <summary>
 /// Generates CSharp text from a expression and writes it to a text writer with the given options.
 /// </summary>
 /// <param name="expression">The expressoin 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 CodeExpression expression, TextWriter writer, CodeGeneratorOptions options)
 {
     using (var provider = new CSharpCodeProvider())
     {
         provider.GenerateCodeFromExpression(expression, writer, options);
     }
 }
开发者ID:DzmitrySo,项目名称:mc,代码行数:13,代码来源:CodeDomGenerator.cs

示例8: ToLiteral

 /// <summary>
 /// Generates full literal representation of a string 
 /// </summary>
 /// <param name="input">The string input, like "hello,\t\".NET\""</param>
 /// <returns>The full literal of the string, like "\"hello,\\t\\\".NET\\\"\""</returns>
 public static string ToLiteral(string input)
 {
     var writer = new StringWriter();
     var codeGen = new CSharpCodeProvider();
     codeGen.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, null);
     return writer.ToString();
 }
开发者ID:RongfangWang,项目名称:ValidationTool,代码行数:12,代码来源:StringHelper.cs

示例9: CreateLiteral

        public string CreateLiteral(object value)
        {
            if (value == null)
            {
                return string.Empty;
            }

            Type type = value.GetType();
            if (type.IsEnum)
            {
                return type.FullName + "." + value.ToString();
            }
            if (type == typeof(Guid))
            {
                return string.Format(CultureInfo.InvariantCulture, "new Guid(\"{0}\")",
                                     ((Guid)value).ToString("D", CultureInfo.InvariantCulture));
            }
            else if (type == typeof(DateTime))
            {
                return string.Format(CultureInfo.InvariantCulture, "new DateTime({0}, DateTimeKind.Unspecified)",
                                     ((DateTime)value).Ticks);
            }
            else if (type == typeof(byte[]))
            {
                var arrayInit = string.Join(", ", ((byte[])value).Select(b => b.ToString(CultureInfo.InvariantCulture)).ToArray());
                return string.Format(CultureInfo.InvariantCulture, "new Byte[] {{{0}}}", arrayInit);
            }
            else if (type == typeof(DateTimeOffset))
            {
                var dto = (DateTimeOffset)value;
                return string.Format(CultureInfo.InvariantCulture, "new DateTimeOffset({0}, new TimeSpan({1}))",
                                     dto.Ticks, dto.Offset.Ticks);
            }

            var expression = new CodePrimitiveExpression(value);
            var writer = new StringWriter();
            CSharpCodeProvider code = new CSharpCodeProvider();
            code.GenerateCodeFromExpression(expression, writer, new CodeGeneratorOptions());
            return writer.ToString();
        }
开发者ID:KCL5South,项目名称:KCLTextTemplating,代码行数:40,代码来源:CodeGenerationTools.cs

示例10: GenerateCodeFromExpression

        private string GenerateCodeFromExpression(CodeExpression expression)
        {
            string code = string.Empty;
            using (CSharpCodeProvider csc = new CSharpCodeProvider())
            using (StringWriter sw = new StringWriter())
            {
                csc.GenerateCodeFromExpression(expression, sw, new CodeGeneratorOptions());
                code = sw.ToString();
            }

            return code;
        }
开发者ID:bitsummation,项目名称:pickaxe,代码行数:12,代码来源:Visitor.WhereStatement.cs

示例11: SerializeValue

        protected string SerializeValue(object value)
        {
            if (value == null)
            {
                // code generation for null - nothing fancy here
                return "null";
            }

            var type = value.GetType();

            if (this.typeSerializers.ContainsKey(type.FullName))
            {
                // code generation by custom handler
                return this.typeSerializers[type.FullName](value);
            }
            else
            {
                try
                {
                    if (value is Enum)
                    {
                        // code generation for enums
                        return type.FullName + "." + Enum.GetName(type, value);
                    }
                    else
                    {
                        // code generation using codedom - works for most simple types
                        var w = new StringWriter();
                        var e = new CodePrimitiveExpression(value);
                        var o = new CodeGeneratorOptions();
                        var p = new CSharpCodeProvider();
                        p.GenerateCodeFromExpression(e, w, o);
                        return w.ToString();
                    }
                }
                catch (ArgumentException ex)
                {
                    throw new NotImplementedException("Cannot serialize value of type " + type.Name + ".", ex);
                }
            }
        }
开发者ID:uuf6429,项目名称:ScintillaNET-Kitchen,代码行数:41,代码来源:MainForm.cs


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