4Manuals

  • PDF Cloud HOME

传递给DynamicInvoke对象转换错误的匿名类型 Download

    获取Hough线的交点OpenCV C ++ 如何将字符串“ text1”设置为包含项目text1,text2,test3的组合框的选定项目? 为什么System.Net.Mail.MailAddress构造函数在域部分中解析带有斜杠“ /”的电子邮件? Android 10,API29:在应用程序文件夹中使用C ++库编写文件会使应用程序崩溃 Unity-LineRenderer没有出现在版本(2D)上 按下按钮后,电话中未显示通知 ASP.net,使用WebConfig httpErrors重定向到区域内的控制器不起作用 获取会议室详细信息 我们如何将参数Line转换为Station? 网络核心:查找实体框架核心的主键和反射

我正在测试一个想法,希望它可以使我将一个数据源(具有不同的字段/数据类型;例如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 个答案:

没有答案



Similar searches
    在nuxt.js上添加暗模式切换以vuetify应用v2.0 表单向表单提交数据后,向表单中的事件添加数字 在php Laravel中从控制器上传之前如何显示图像预览 如何使用带有自定义值类型的Go容器? 检测单词然后发送消息(discord.py)