3

I'm attempting to use a static field of type ImmutableArray<T> in a struct where T is the type of the struct. However, when I attempt to do this, I get an immediate TypeLoadException when I execute the program. This appears to occur before the program itself even begins to run. There is no stack trace available and I can't step through anything. Here is a simple example that illustrates the problem:

namespace Alpha
{
    using System;
    using System.Collections.Immutable;

    internal class Program
    {
        private static void Main(string[] args)
        {
            var simple = new Simple(0);
            Console.WriteLine(simple.Index);
            Console.ReadLine();
        }

        public struct Simple
        {
            public static readonly ImmutableArray<Simple> Values =
                ImmutableArray<Simple>.Empty;

            public Simple(int index)
            {
                Index = index;
            }

            public int Index { get; }
        }
    }
}

Here's the error code I get: System.TypeLoadException was unhandled
Message: An unhandled exception of type 'System.TypeLoadException' occurred in mscorlib.dll Additional information: Could not load type 'Alpha.Program+Simple' from assembly 'Alpha, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

If I don't initialize the field by removing the ImmutableArray<Simple>.Empty it still fails to run. However, if I change the static field to either of the following, the program runs just fine:

public static readonly ImmutableList<Simple> Values = ImmutableList<Simple>.Empty;  
public static readonly Simple[] Values = new Simple[0];

I'm using Visual Studio Community 2015 and System.Collections.Immutable Version 1.1.37.

Mike Cowan
  • 919
  • 5
  • 11

0 Answers0