Bug 53 - Obtaining FieldInfo from expression in LINQ ConstantExpression returns null.
Summary: Obtaining FieldInfo from expression in LINQ ConstantExpression returns null.
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: unspecified
Hardware: All All
: Lowest trivial
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-07-22 18:10 UTC by Andrew Finnell
Modified: 2014-01-12 01:02 UTC (History)
3 users (show)

Tags: linq
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Andrew Finnell 2011-07-22 18:10:39 UTC
Unit Test (Passes on .NET fails on Mono 2.10.1):

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using NUnit.Framework;

namespace MonoLinq
{
    [TestFixture]
    public class TestLinqConstantExpression
    {
        void UseNonConstMemberVariable<T>(IEnumerable<T> data, Expression<Func<T, Boolean>> input)
        {            
            var expression = (BinaryExpression)input.Body;
            var memberExpression = (MemberExpression) expression.Right;
            var constantExpression = (ConstantExpression) memberExpression.Expression;
            var memberInfo = constantExpression.Type;
            var fieldInfo = memberInfo.GetField(memberExpression.Member.Name);
            Assert.IsNotNull(fieldInfo);
        }

        [Test]
        public void MonoLinqSupportsNonConstMemberFieldInExpression()
        {
            var thisHasToBeNonConst = "a";
            var items = new List<String>();
            UseNonConstMemberVariable(items, item => item == thisHasToBeNonConst);
        }
    }    
}

Mono Version:

Mono JIT compiler version 2.10.1 (tarball)
Copyright (C) 2002-2011 Novell, Inc and Contributors. www.mono-project.com
        TLS:           normal
        SIGSEGV:       normal
        Notification:  Thread + polling
        Architecture:  x86
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            Included Boehm (with typed GC and Parallel Mark)
Comment 1 Andrew Finnell 2011-07-23 09:46:39 UTC
This appears to be an abuse of LINQ on my part. In .NET constantExpression.Type just happens to be the same MethodInfo of memberExpression.Member. This does not appear to be the case in Mono.

The correct way to use LINQ in this scenario is:

var constantExpression = memberExpression.Expression as ConstantExpression;
var memberInfo = memberExpression.Member;
if (memberInfo is FieldInfo)
{
    var value = ((FieldInfo)memberInfo).GetValue(constantExpression.Value);
}

Downgrading this to trivial.
Comment 2 Rodrigo Kumpera 2014-01-12 01:02:52 UTC
The above works on mono 3.2.5 in the same as on .NET 4.5