- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
private static bool EvaluteExpression(string expression, out object result)
{
string source =
string.Format("public static class Evalutor{{public static object GetValue(){{return {0};}}}}",
expression);
using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
{
System.CodeDom.Compiler.CompilerParameters compilerParams =
new System.CodeDom.Compiler.CompilerParameters
{
GenerateInMemory = true,
GenerateExecutable = false
};
System.CodeDom.Compiler.CompilerResults compileResults = provider.CompileAssemblyFromSource(compilerParams, source);
if (compileResults.NativeCompilerReturnValue == 0)
{
Type type = compileResults.CompiledAssembly.GetType("Evalutor");
MethodInfo method = type.GetMethod("GetValue");
result = method.Invoke(null, null);
return true;
}
}
result = null;
return false;
}