I know that this question has been asked a million times but all seem to refer to missing object or library references. I am declaring the variable "K" with a type of "TYPE". For some reason this will not build.
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();
}
If I comment out the last line of the routine it compiles.
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();
}
When I comment out the last line I am able to build it. When I debug the code with the last line commented I get the same problem when I "watch" the variable as well. I had read that this could be because I was building in Release mode and it was discarding any variables it decide were unnecessary, however I am building in debug mode so ... ??
I feel like I have run into this problem several times over the years and have always "hacked" my way around it. I would like to understand what's happening so that I can resolve the problem rather than come up with some clever work-around.
Thank you