C# 类型或命名空间名称';K';在当前上下文中不存在

C# 类型或命名空间名称';K';在当前上下文中不存在,c#,visual-studio,variables,declare,C#,Visual Studio,Variables,Declare,我知道这个问题已经被问了一百万次了,但似乎都是指缺少的对象或库引用。我声明变量“K”的类型为“type”。由于某些原因,这将无法建立 foreach (var propertyInfo in typeof(T).GetProperties()) { // // Gets the property from the obj MemberExpression propertyExpression

我知道这个问题已经被问了一百万次了,但似乎都是指缺少的对象或库引用。我声明变量“K”的类型为“type”。由于某些原因,这将无法建立

        foreach (var propertyInfo in typeof(T).GetProperties())
        {
            //
            // Gets the property from the obj
            MemberExpression propertyExpression = Expression.Property(Expression.Constant(obj), propertyInfo);
            //
            // get the data type of the property
            Type K = propertyExpression.Member.ReflectedType.UnderlyingSystemType;
            //
            // get the value of the property -- this line throws the error
            var currentValue = Expression.Lambda<Func<K>>(propertyExpression).Compile().Invoke();
        }
foreach(typeof(T).GetProperties()中的var-propertyInfo)
{
//
//从obj获取属性
MemberExpression propertyExpression=Expression.Property(Expression.Constant(obj),propertyInfo);
//
//获取属性的数据类型
类型K=propertyExpression.Member.ReflectedType.UnderlyingSystemType;
//
//获取属性的值--此行抛出错误
var currentValue=Expression.Lambda(propertyExpression.Compile().Invoke();
}
如果我注释掉它编译的例程的最后一行

        foreach (var propertyInfo in typeof(T).GetProperties())
        {
            //
            // Gets the property from the obj
            MemberExpression propertyExpression = Expression.Property(Expression.Constant(obj), propertyInfo);
            //
            // get the data type of the property
            Type K = propertyExpression.Member.ReflectedType.UnderlyingSystemType;
            //
            // get the value of the property
            //var currentValue = Expression.Lambda<Func<K>>(propertyExpression).Compile().Invoke();
        }
foreach(typeof(T).GetProperties()中的var-propertyInfo)
{
//
//从obj获取属性
MemberExpression propertyExpression=Expression.Property(Expression.Constant(obj),propertyInfo);
//
//获取属性的数据类型
类型K=propertyExpression.Member.ReflectedType.UnderlyingSystemType;
//
//获取属性的值
//var currentValue=Expression.Lambda(propertyExpression.Compile().Invoke();
}
当我注释掉最后一行时,我能够构建它。当我调试带有最后一行注释的代码时,当我“监视”变量时也会遇到同样的问题。我读到这可能是因为我是在发布模式下构建的,它丢弃了它认为不必要的任何变量,但是我是在调试模式下构建的,所以

我觉得这些年来我已经遇到过好几次这个问题,并且一直在“破解”这个问题。我想了解发生了什么,这样我就可以解决问题,而不是想出一些聪明的解决办法

多谢各位


这很可能与foreach上下文有关。考虑到它是一个动态上下文,这意味着每次迭代时“K”都将是一个全新的实例,Expression.Lambda语句可能拒绝使用K(可能它需要像类一样的常量,不确定)。尝试改用for和编号索引,并告诉我们结果。

这很可能与foreach上下文有关。考虑到它是一个动态上下文,这意味着每次迭代时“K”都将是一个全新的实例,Expression.Lambda语句可能拒绝使用K(可能它需要像类一样的常量,不确定)。尝试改用for和编号的索引,并告诉我们结果。

当前值的类型是什么?在编译过程中,编译器必须确定这一点。但您试图包含一个只在运行时解析的表达式。那是行不通的。对运行时类型信息的引用不能用于预期使用类型本身的位置。在运行时之前,不知道可能存在重复的类型。它可以是字符串,也可以是自定义类/对象定义。也许我所尝试的无法实现,但为什么propType被报道为根本不存在呢?即使我不能这样做,这能解释为什么propType声明失败吗?
currentValue的类型是什么?在编译过程中,编译器必须确定这一点。但您试图包含一个只在运行时解析的表达式。那是行不通的。对运行时类型信息的引用不能用于预期使用类型本身的位置。在运行时之前,不知道可能存在重复的类型。它可以是字符串,也可以是自定义类/对象定义。也许我所尝试的无法实现,但为什么propType被报道为根本不存在呢?即使我不能这样做,这能解释为什么propType声明失败吗?谢谢你的建议,但是在更改为for循环后,我仍然观察到相同的问题。谢谢你的建议,但是在更改为for循环后,我仍然观察到相同的问题。
Expression.Lambda<Func<K>>(…)
// theObject is the input
foreach (var pi in theObject.GetType().GetProperties()) {
  object val = pi.GetValue(theObject);
  // do something with val
}