publicstaticvoidMain(string[] args) { var lambdaAnswer = Add(3, 3, (a, b) => a + b); //传入lambda表达式1 var lambdaAnswer1 = Add("args", 3, (a, b) => a + b.ToString());//传入lambda表达式2 var lambdaAnswer1 = Add(4, 3.4m, (a, b) => a + (int)b);//传入lambda表达式3 }
publicstaticvoidMain(string[] args) { List<string> strList = new List<string>(); //是System.Object含有的那些成员,不会报无效转换异常。 strList.Add("aaa");
var results = strList.Cast<string>();
foreach (var item in results) { Console.WriteLine(item); } }
解决办法1:
1 2 3 4 5
var results = from MyType v in strList select v; 换成 var results = from v in strList select (MyType)v; //select方法接受的是lambda表达式,对于v来说,lambda表达式是string类型的对象
解决办法2:strList.Select(a => new MyType { StringMember = a });
namespaceConsoleApp1 { //一个简单的例子:服务方法的参数是常量,打印方法名,参数类型,参数值。 classProgram { publicstaticvoidMain(string[] args) { var client = new ClientProxy<IService>(); client.CallInterface<string>(server=>server.DoWork(666)); } }
public class ClientProxy<T> { public TResult CallInterface<TResult>(Expression<Func<T,TResult>> op) { var exp = op.Body as MethodCallExpression; //静态方法或实例方法的调用 var methodName = exp.Method.Name; var methodInfo = exp.Method; var allParameters = from element in exp.Arguments selectProcessArgument(element);
Console.WriteLine($"Calling {methodName}");
foreach(var param in allParameters) { Console.WriteLine($"\tParameter type={param.ParamType} value={param.ParamValue}"); } //如何动态的传入参数 ?????????????????????? //???????????????????????????? //var result = op.Compile();// (()allParameters.First().ParamValue); returndefault(TResult); }
classProgram { publicstaticvoidMain(string[] args) { var converter = new Converter<Source,Dest>(); Source source = new Source(); source.AA = "AA"; source.BB = 22; source.CC = 66; source.EE = 88; var dest = converter.ConvertFrom(source); } }
public class Converter<TSource, TDest> { Func<TSource, TDest> converter; public TDest ConvertFrom(TSource source) { if (converter == null) { CreateConverter(); } return converter(source); }
privatevoidCreateConverter() { //创建一个ParameterExpression节点,标识表达式树中的参数或变量 var source = Expression.Parameter(typeof(TSource), "source"); //创建一个ParameterExpression节点,标识表达式树中的参数或变量 var dest = Expression.Variable(typeof(TDest), "dest");
var assignments = from srcProp in typeof(TSource). GetProperties(BindingFlags.Public | BindingFlags.Instance) where srcProp.CanRead let destProp = typeof(TDest).GetProperty(srcProp.Name, BindingFlags.Public | BindingFlags.Instance) where (destProp != null) && (destProp.CanWrite) select Expression.Assign(Expression.Property(dest, destProp), Expression.Property(source, srcProp));
var body = new List<Expression>(); body.Add(Expression.Assign(dest,Expression.New(typeof(TDest)))); body.AddRange(assignments); body.Add(dest);
var expr = Expression.Lambda<Func<TSource, TDest>>(Expression.Block(new[] { dest},body.ToArray()),source);
namespaceConsoleApp1 { classProgram { publicstaticvoidMain(string[] args) { var data = new CSVDataContainer(new System.IO.StreamReader(@"C:\Users\bibi\Desktop\代码\异步\ConsoleApp1\TextFile1.txt")); foreach(var item in data.Rows) { Console.WriteLine($"{item.列1}{item.列2}{item.列3}"); }