简体   繁体   中英

Getting MethodAccessException when using LinqBridge

I am attempting to use LinqBridge with Compact Framework 2.0 but I get a MethodAccessException when a Linq Where method is called.

First I tried using the pre-compiled assembly from BitBucket , my own fork with some minor tweeks to compile for CF, and ctacke's fork from his sdf repo on gitHub.

I've been careful about cleaning between builds and cleaning the deployment dir on the device. Also I've been decompiling using DotPeek to confirm that everything in LinqBridge that should be public is showing up as public.

Update 1

Before I was using the following code and it was working fine

public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
    foreach (TSource source1 in source)
    {
        if (predicate(source1))
          yield return source1;
    }
}

Update 2

The following code throws a MethodAccessException when ran using compact framework 2.0 (using both the precompiled dll from raboof and ctacke's fork), but not in Compact Famework 3.5 (using LinqBridge instead of System.Core), or Full Framework 2.0. So I suspect this is a bug in the Compact Framework 2.0 CLR.

using System.Linq;
static class Program
{
    private enum Something { a, b, c };

    [MTAThread]
    static void Main()
    {
        var dict = new Dictionary<Something, Something>();
        dict.Add(Something.a, Something.a);
        dict.RemoveByValue(Something.a);
    }
}

public static class DictionaryExtentions
{
    public static void RemoveByValue<TKey, TValue>(this IDictionary<TKey, TValue> dict, TValue value)
    {
        foreach (var item in dict.Where(kvp => kvp.Value.Equals(value)).ToArray())
        {
            dict.Remove(item.Key);
        }
    }
}

The problem seems to be due to using a private type as the TSource in a Where call. This is a bit above my level of understanding but I suspect the issue might be due to compiler generated code known as Display Classes that are used to wrap lambdas. If anyone has a better understanding of this feel free to post a answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM