This project is read-only.

System.InvalidProgramException : Common Language Runtime detected an invalid program

Sep 5, 2012 at 7:07 AM
Edited Sep 5, 2012 at 7:08 AM

I'm trying to serialize a class and get this error:

System.InvalidProgramException : Common Language Runtime detected an invalid program.
   at _(Object)
   at fastJSON.JSONSerializer.WriteObject(Object obj) in JsonSerializer.cs: line 330
   at fastJSON.JSONSerializer.WriteValue(Object obj) in JsonSerializer.cs: line 109
   at fastJSON.JSONSerializer.ConvertToJSON(Object obj) in JsonSerializer.cs: line 29
   at fastJSON.JSON.ToJSON(Object obj, JSONParameters param) in JSON.cs: line 90
   at fastJSON.JSON.ToJSON(Object obj) in JSON.cs: line 74

The line is:

List<Getters> g = Reflection.Instance.GetGetters(t);
            foreach (var p in g)
                if (append)
                object o = p.Getter(obj);


The class in question is ClassB. Like this:


public class ClassA<T>
	private Dictionary<string, T> dictionary = new Dictionary<string, T>();
	private static object SyncObject = new object();

public class ClassB: ClassA<String>, SomeInterface

Sep 9, 2013 at 6:05 PM
As the documentation on the codeproject page says:
InvalidProgramException is thrown when property indexer (aka this[]) is defined on class to serialize
This can be fixed by adding the following line:
if (p.GetIndexParameters().Length > 0) continue;    // Property is indexer.
In the file Reflection.cs right after:
internal List<Getters> GetGetters(Type type)
            List<Getters> val = null;
            if (_getterscache.TryGetValue(type, out val))
                return val;

            PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            List<Getters> getters = new List<Getters>();
            foreach (PropertyInfo p in props)
                if (!p.CanWrite && ShowReadOnlyProperties == false) continue;
                ..... < -- Here -- > ...
This fix recognizes if the property is indexer and skips it. This is of course in case you don't wanna serialize the indexed data, which I think is correct (since serialization doesn't know how many indexes you have).
Dec 4, 2013 at 6:58 AM
I think this should be added to the master branch code. Indexers cannot be serialized by fastJSON. It is simply not supported right now. That is why this exception occurs.

It breaks exactly in this line of code:
object o = p.Getter(obj);
And just for the indexer which is identified as a property with the name of "Item" when calling the type via reflection. It doesn't really exist in the instance of the class, the indexer is being used differently.

This is effectively a bug in fastJSON.