本文整理汇总了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();
}
示例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();
}
示例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();
}
示例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();
}
}
}
示例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();
}
示例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();
}
}
示例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);
}
}
示例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();
}
示例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();
}
示例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;
}
示例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);
}
}
}