C#使用表达式树不能包含动态操作,使用反射的方式来实现T类型

今天 遇到一个类:

class FrmGenerateEdit<T> : Form where T : class, new()

T是一个数据库的表对应的Model,比如数据库表Employee对应的类,该类设定会有一个主键自增的属性Property(或者字段Field)名字为CoreId,

现在要做设计一个函数GetEmployeeByCoreId(T t,int coreId),考虑到dynamic关键字,尝试使用它。

输入代码如下:

Expression<Func<T, bool>> expression = t => ((dynamic)t).CoreId == 5;

如何才能实现dynamic.CoreId使其编译通过;

弹出错误:

可以使用:

Expression<Func<T, bool>> expression = t => GetExpression(t, coreId); //可编译通过,运行后尝试        

        private bool GetExpression(T t, int coreIdTemp)
        {
            dynamic tt = t;
            return tt.CoreId == coreIdTemp;
        }

 

新建控制台C#应用程序ExpressionAndDynamicDemo,输入测试代码.

如下:

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ExpressionAndDynamicDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("----测试对Dynamic的使用----");
            dynamic x = 66; 
            dynamic result = ExtensionInt32.GetDynamic(x); 
            Console.WriteLine(result);

            Console.WriteLine("----测试表达式树中的dynamic使用----");
            Business<Employee> business = new Business<Employee>();
            business.Test(3);

            business.Test(4);
            Console.ReadLine();
        }
    }

    class Business<T> where T : class, new() 
    {
        /// <summary>
        /// 测试
        /// </summary>
        /// <param name="coreId"></param>
        public void Test(int coreId) 
        {
            Expression<Func<T, bool>> expression = t => GetExpression(t, coreId); //可编译通过,运行后尝试
            bool compareResult = Where(expression);
            Console.WriteLine($"是否存在【CoreId={coreId}】的员工信息:{compareResult}");
        }

        /// <summary>
        /// Where筛选器
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public bool Where(Expression<Func<T, bool>> expression) 
        {
            Func<T, bool> func = expression.Compile();
            Type type = typeof(T);
            object instanceObj = Activator.CreateInstance(type);
            PropertyInfo propertyCoreId = type.GetProperty("CoreId");
            propertyCoreId.SetValue(instanceObj, 3);

            PropertyInfo propertyEmployeeName = type.GetProperty("EmployeeName");
            propertyEmployeeName.SetValue(instanceObj, "云无月");

            PropertyInfo propertyAddress = type.GetProperty("Address");
            propertyAddress.SetValue(instanceObj, "天鹿城");
            bool compareResult = func((T)instanceObj);
            if (compareResult)
            {
                Console.WriteLine($"该员工信息是:{instanceObj}");
            }
            return compareResult;
        }

        /// <summary>
        /// 使用dynamic表达式
        /// </summary>
        /// <param name="t"></param>
        /// <param name="coreIdTemp"></param>
        /// <returns></returns>
        private bool GetExpression(T t, int coreIdTemp) 
        {
            dynamic temp = t;
            return temp.CoreId == coreIdTemp;
        }

    }

    /// <summary>
    /// 员工信息表
    /// </summary>
    class Employee 
    {
        /// <summary>
        /// 唯一主键编号
        /// </summary>
        public int CoreId { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string EmployeeName { get; set; }

        /// <summary>
        /// 地址
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 打印员工信息
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return $"{
   
   {\"编号\":{CoreId},\"姓名\":{EmployeeName},\"地址\":{Address}}}";
        }
    }

    /// <summary>
    /// 对Int32的扩展类
    /// </summary>
    static class ExtensionInt32 
    {
        /// <summary>
        /// 对int的扩展函数Hello
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public static dynamic Hello(this int x) 
        { 
            return $"Hello:{x}"; 
        }

        /// <summary>
        /// 返回动态结果
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static dynamic GetDynamic(int t) 
        {
            return t.Hello(); 
        }
    }
}

测试运行如图:

 

 

 

Guess you like

Origin blog.csdn.net/ylq1045/article/details/113740037