c#获取父类_C#动态生成代码,有继承父类,传入父类计算得到结果,看代码

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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值