Unity-Technologies/UnityCsReference · error · FormatException

Expected {o} to be a string[] or a Type[]. It is a {o.GetTyp

Error message

Expected {o} to be a string[] or a Type[]. It is a {o.GetType()}

What it means

Thrown by the private TypesOrTypeNames helper when its input is neither a Type[] nor a string[]. It mirrors TypeOrTypeName but for arrays used in parameter-type resolution; any other runtime type produces a FormatException.

Source

Thrown at Editor/Mono/Macros/MethodEvaluator.cs:151

                    return t;
                case string s:
                    return Type.GetType(s);
                default:
                    throw new FormatException($"Expected {o} to be a string or a Type. It is a {o.GetType()}");
            }
        }

        private static Type[] TypesOrTypeNames(object o)
        {

            switch (o)
            {
                case Type[] t:
                    return t;
                case string[] s:
                    return Array.ConvertAll(s, Type.GetType);
                default:
                    throw new FormatException($"Expected {o} to be a string[] or a Type[]. It is a {o.GetType()}");
            }
        }

        public static object ExecuteExternalCode(string parcel)
        {
            EvalDataParcel evalDataParcel;
            // Process EvalDataParcel to extract known types information which is required for
            // deserialization of the main EvalRemoteMethodCallParcel data.
            using (var dataStream = new MemoryStream(Convert.FromBase64String(parcel)))
            {
                // Setting quotas to max to avoid issues with large data.
                using var reader = XmlDictionaryReader.CreateTextReader(dataStream, XmlDictionaryReaderQuotas.Max);
                var serializer = new DataContractSerializer(typeof(EvalDataParcel));
                try
                {
                    evalDataParcel = (EvalDataParcel)serializer.ReadObject(reader, true);
                }
                catch (SerializationException)

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Regenerate the eval parcel with matching versions so parameter-type arrays serialize as Type[] or string[].
  2. Normalize the input to Type[] before calling the evaluator.
  3. Verify the MethodEvaluatorSurrogateProvider round-trips the array shape correctly.
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(o is Type[]) && !(o is string[])) throw new FormatException($"Expected Type[] or string[], got {o?.GetType()}");

Type guard

static bool IsTypeOrTypeNameArray(object o) => o is Type[] || o is string[];

Try / catch

try { /* evaluator call */ }
catch (FormatException ex) when (ex.Message.Contains("string[] or a Type[]")) { Debug.LogError($"Bad param-types entry: {ex.Message}"); }

Prevention

When it happens

Trigger: A deserialized eval payload element expected to be an array of types resolves to a different shape — a single Type/string instead of an array, an object[], or a list type that does not pattern-match Type[]/string[].

Common situations: Version skew between the parcel producer and consumer, or a serialization surrogate that converts the array into a non-matching representation.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/8815c2da195d6007. Report an issue: GitHub.