Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建动态表达式<;Func<;T、 Y>&燃气轮机;_C#_.net_Linq - Fatal编程技术网

C# 创建动态表达式<;Func<;T、 Y>&燃气轮机;

C# 创建动态表达式<;Func<;T、 Y>&燃气轮机;,c#,.net,linq,C#,.net,Linq,我想创建一个动态表达式。以下代码适用于字符串,但不适用于日期时间。我的意思是,我有一个例外: “类型为'System.Nullable'1[System.DateTime]'的表达式不能为空。” 用于返回类型“System.Object” 任何人都能分析这个错误吗 Type type = typeof(DSVPNProjection); ParameterExpression arg = Expression.Parameter(type, "x");

我想创建一个动态
表达式
。以下代码适用于字符串,但不适用于日期时间。我的意思是,我有一个例外:

“类型为'System.Nullable'1[System.DateTime]'的表达式不能为空。” 用于返回类型“System.Object”

任何人都能分析这个错误吗

        Type type = typeof(DSVPNProjection);
        ParameterExpression arg = Expression.Parameter(type, "x");
        Expression expr = arg;

        PropertyInfo propertyInfo = type.GetProperty(sidx);
        expr = Expression.Property(expr, propertyInfo);

        var expression = 
        Expression.Lambda<Func<DSVPNProjection, object>>(expr, arg);
Type Type=typeof(DSVPN项目);
ParameterExpression arg=Expression.Parameter(类型为“x”);
表达式expr=arg;
PropertyInfo PropertyInfo=type.GetProperty(sidx);
expr=Expression.Property(expr,propertyInfo);
变量表达式=
Lambda(expr,arg);

是否需要将
对象
更改为其他类型?如果是,那么是哪一个?正如您所见,我正在尝试动态获取PropertyInfo并将其用作Func中的第二个参数。

对于值类型,您需要显式执行装箱(即转换为
对象
):

Type Type=typeof(DSVPN项目);
ParameterExpression arg=Expression.Parameter(类型为“x”);
表达式expr=null;
PropertyInfo PropertyInfo=type.GetProperty(sidx);
expr=Expression.Property(arg,propertyInfo);
if(propertyInfo.PropertyType.IsValueType)
expr=Expression.Convert(expr,typeof(object));
变量表达式=
Lambda(expr,arg);

它究竟是如何不起作用的?它会引发异常吗?消息是什么?对我有用,谢谢,尽管“expr=Expression.Property(expr,propertyInfo);”需要调整为“expr=Expression.Property(arg,propertyInfo);”,因为expr此时始终为空
    Type type = typeof(DSVPNProjection);
    ParameterExpression arg = Expression.Parameter(type, "x");
    Expression expr = null;

    PropertyInfo propertyInfo = type.GetProperty(sidx);
    expr = Expression.Property(arg, propertyInfo);
    if (propertyInfo.PropertyType.IsValueType)
        expr = Expression.Convert(expr, typeof(object));

    var expression = 
    Expression.Lambda<Func<DSVPNProjection, object>>(expr, arg);