usingSystem;usingSystem.CodeDom.Compiler;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceCw_Expression
{classProgram
{static void Main(string[] args)
{string exp = "ToRound(ToDouble(I1),2)";
Test t1= newTest();
t1.I1= "1.9876";try{var s = typeof(Test1).GetMethod("GetD");var d = s.Invoke(t1, null);
}catch(Exception ex)
{
}#region MyRegion
var result = Expression(exp, t1);
Console.WriteLine(result);#endregionConsole.ReadKey();
}#region MyRegion
static Result Expression(string exp, Source t) where Source : class, new()
{var source = typeof(Source);var result = typeof(Result);
CodeDomProvider codeDomProvider= CodeDomProvider.CreateProvider("CSharp");
CompilerParameters compilerParameters= newCompilerParameters();
compilerParameters.ReferencedAssemblies.Add("System.dll");
compilerParameters.ReferencedAssemblies.Add(source.Assembly.Location);
compilerParameters.GenerateInMemory= true;
compilerParameters.TreatWarningsAsErrors= false;var namespace_Name = "Dynamic_" + Guid.NewGuid().ToString("N");var class_Name = "DynamicClass_" + Guid.NewGuid().ToString("N");var method_Name = "DynamicMethod_" + Guid.NewGuid().ToString("N");var field_Name =result.FullName;if(result.IsGenericType)
{
Type genericTypeDefinition=result.GetGenericTypeDefinition();if (genericTypeDefinition == typeof(Nullable<>))
{
Type underlyingType=Nullable.GetUnderlyingType(result);
field_Name= underlyingType.FullName + "?";
}else{
Type[] genericArguments=result.GetGenericArguments();
field_Name= genericTypeDefinition.FullName.Substring(0, genericTypeDefinition.FullName.Length - 2) + "
Type[] array=genericArguments;foreach (Type type inarray)
{
field_Name+=type.FullName;
}
field_Name+= ">";
}
}
StringBuilder sb= newStringBuilder();
sb.Append("using System;").AppendLine();
sb.Append("using").Append(source.Namespace).Append(";").AppendLine();
sb.Append("namespace").Append(namespace_Name).AppendLine();
sb.Append("{");
sb.Append("public class").Append(class_Name).Append(":").Append(source.FullName).AppendLine();
sb.Append("{").AppendLine();
sb.Append("public").Append(field_Name).Append(" ").Append(method_Name).Append("()").AppendLine();
sb.Append("{").AppendLine();
sb.Append("return").Append(exp).Append(";").AppendLine();
sb.Append("}").AppendLine();
sb.Append("}").AppendLine();
sb.Append("}");
CompilerResults compilerResults=codeDomProvider.CompileAssemblyFromSource(compilerParameters, sb.ToString());if (compilerResults.Errors.Count > 0)
{//编译异常
}//Type type2 = compilerResults.CompiledAssembly.GetType(namespace_Name + "." + class_Name);
var dd = compilerResults.CompiledAssembly.CreateInstance(namespace_Name + "." +class_Name);var ddd =dd.GetType().GetMethod(method_Name);var dddd = ddd.Invoke(t, null);return dddd == null ? default(Result) : (Result)dddd;
}#endregion}public classTest {public string I1 { get; set; }public double ToDouble(stringi1)
{return double.Parse(i1);
}public double ToRound(double data, intnum)
{returnMath.Round(data, num);
}
}public classTest1 : Test {public doubleGetD()
{return ToRound(ToDouble(I1), 2);
}
}
}