我正在测试一个想法,希望它可以使我将一个数据源(具有不同的字段/数据类型;例如 最终目标是能够从某些数据源查询表达式字符串(使用 我有一个强类型类的工作示例,但是使用匿名类型时收到以下错误:
我已经搜索了一段时间,但还没有提出任何建议,在这一点上,我可以自信地说这已经达到我公认的有限的 下面是所有相关代码。 强类型输入(有效)示例: 匿名类型(不工作)示例(请参见代码正文中的错误): 阵列扩展方法: DataReader
,一个2d数组,一个字典等)强制转换为{ {1}},其中List<T>
是使用引用扩展名的扩展方法创建的匿名类型
T
。创建对象后,我正在使用现有的流畅接口(https://www.codeproject.com/Articles/1079028/Build-Lambda-Expressions-Dynamically)
创建可以转换为字符串并保存到数据库,xml文件等以供以后使用的lambda表达式。 System.Linq.Dynamic.Core
作为参数),加载原始anontype
,将表达式字符串编译为有效的lambda ,然后调用它。想要以下内容(用伪代码):
string exp =“来自数据源的一些lambda字符串” List<anontype>
var anontype = dataFields.ToAnonType(); //field names of some dataset
List<anontype> annonList = dataSet.ToList();
var param = Expression.Parameter(annonType.GetType(), ExpressionParameterName.Parent);
var lambdaFromString = System.Linq.Dynamic.DynamicExpression.ParseLambda(new[] { param }, null, exp);
var compiledExp = lambdaFromString.Compile();
var result = compiledExp.DynamicInvoke(anonList);
1 [System.Object]'无法转换为类型' f__AnonymousType0 Object of type 'System.Collections.Generic.List
2[System.String,System.String]'..
知识的程度。 .NET
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ExpressionBuilder.Generics;
using ExpressionBuilder.Common;
using System.Linq;
namespace ExpressionBuilterTest
{
class Program
{
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Industry { get; set; }
public string Location { get; set; }
}
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>();
Customer customer = new Customer();
customer.Id = 1;
customer.Name = "Alex";
customer.Industry = "Mining"
customer.Location = "Harlan, KY"
customers.Add(customer);
var filter = new Filter<Customer>();
filter.By("Id", Operation.Between, 2, 4)
.Or.By("Name", Operation.Contains, " Alex");
var lamda = filter.GetExpression();
//convert to string
string exp = lamda.Body.ToString();
exp = exp.Replace("AndAlso", "&&").Replace("OrElse", "||");
// simulation of compiling a serialized expression
var param = Expression.Parameter(typeof(Customer), ExpressionParameterName.Parent);
var lambdaFromString = System.Linq.Dynamic.DynamicExpression.ParseLambda(new[] { param }, null, exp);
var compiledExp = lambdaFromString.Compile();
var result = compiledExp.DynamicInvoke(customers);
Console.WriteLine(result); //this prints "false" which is correct
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ExpressionBuilder.Generics;
using ExpressionBuilder.Common;
using System.Linq;
namespace ExpressionBuilterTest
{
class Program
{
static void Main(string[] args)
{
object[,] arrayTest = new object[3, 2];
arrayTest[0, 0] = "Field1";
arrayTest[1, 0] = "X1";
arrayTest[2, 0] = "Y1";
arrayTest[0, 1] = "Field2";
arrayTest[1, 1] = "X2";
arrayTest[2, 1] = "Y2";
var anonList = arrayTest.CreateClass().ToList(); // see "Array Extension Method" below
Type type = anonList[0].GetType();
Type genericClass = typeof(Filter<>);
Type constructedClass = genericClass.MakeGenericType(type);
dynamic filter = Activator.CreateInstance(constructedClass);
filter.By("Field1", Operation.Contains, " X1 ")
.Or.By("Field2", Operation.Contains, " X2 ");
var lamda = filter.GetExpression();
string exp = lamda.Body.ToString();
exp = exp.Replace("AndAlso", "&&").Replace("OrElse", "||");
var param = Expression.Parameter((Type)lamda.Parameters[0].Type, ExpressionParameterName.Parent);
var compiledExp = lambdaFromString.Compile();
//*******************************************************
//Error Here
var result = compiledExp.DynamicInvoke(anonList); // Error: Object of type 'System.Collections.Generic.List`1[System.Object]' cannot be converted to type '<>f__AnonymousType0`2[System.String,System.String]'.
//*******************************************************
Console.WriteLine(result);
Console.ReadKey();
}
}
}
0 个答案:
没有答案