Project

General

Profile

Bug #13670

ConfigNode.ReadArray breaks on trying to load IEnumerable that is not an IList.

Added by allista about 7 years ago.

Status:
New
Severity:
Normal
Assignee:
-
Category:
Application
Target version:
-
Start date:
01/26/2017
% Done:

0%

Version:
Platform:
Linux, OSX, PS4, Windows, XBoxOne
Expansion:
Language:
English (US)
Mod Related:
No
Votes:
Arrow u r green
Arrow d r red

Description

ConfigNode.IsGenericArray returns true for any IEnumerable<> field:

Type type = typeof(IEnumerable<>).MakeGenericType(new Type[] { genericArguments[i] });
if (type.IsAssignableFrom(fieldType))
{
    return true;
}

But any of the Read...Array methods assume that such a field can be casted to IList:
if (ConfigNode.IsGenericArray(field.FieldType))
{
   IList list2 = (IList)Activator.CreateInstance(field.FieldType);

This breaks, for example, the loading of an object containing: [Persistent] HashSet<int> some_set:

InvalidCastException: Cannot cast from source type to destination type.
  at ConfigNode.ReadValueArray (. field, .ConfigNode node)
  at ConfigNode.ReadArray (. field, .ConfigNode node)
  at ConfigNode.ReadObject (System.Object obj, .ConfigNode node)
  at ConfigNode.LoadObjectFromConfig (System.Object obj, .ConfigNode node, Int32 pass, Boolean removeAfterUse)
  at ConfigNode.LoadObjectFromConfig (System.Object obj, .ConfigNode node)

Solution: IsGenericArray should check for IList or, better yet, for ICollection instead of IEnumerable.

Also available in: Atom PDF