Path: redb/redb.Core/Exceptions/RedbNestedCollectionException.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Exceptions/RedbNestedCollectionException.cs
----
using System;
using System.Linq;
namespace redb.Core.Exceptions;
///
/// A property holds a collection directly inside a collection (List<List<T>>, T[][],
/// List<T[]>, List<Dictionary<K, V>>, Dictionary<K, List<V>>,
/// Dictionary<K, Dictionary<K2, V>>, Dictionary<K, V>[]) or is a multi-dimensional array
/// (T[,]). redb stores neither: before, each inner collection was written as the name of its type, a
/// multi-dimensional array as a flat one, and the object could not be loaded afterwards. The scheme sync refuses such a
/// property before it writes anything; a save that meets one (a scheme synced before the refusal existed) refuses
/// before any row is deleted or written. A byte[] element or value is a single value and is not refused.
/// Wrap the inner collection in a class: List<Group> with class Group { List<T> Items }.
///
public class RedbNestedCollectionException : Exception
{
/// The class (or scheme) that declares the property.
public string OwnerName { get; }
/// The property.
public string PropertyName { get; }
/// The declared type of the property, or what the save met in it, in C# notation.
public string PropertyTypeName { get; }
/// Refusal of a declared property type, at the scheme sync.
public RedbNestedCollectionException(Type ownerType, string propertyName, Type propertyType)
: this(Display(ownerType), propertyName, Display(propertyType), atSave: false)
{
}
/// Refusal at a save: says what the save met in the property.
public RedbNestedCollectionException(string ownerName, string propertyName, string found)
: this(ownerName, propertyName, found, atSave: true)
{
}
private RedbNestedCollectionException(string ownerName, string propertyName, string typeName, bool atSave)
: base((atSave
? $"The save is refused: the property '{ownerName}.{propertyName}' holds {typeName}. "
: $"The property '{ownerName}.{propertyName}' of type '{typeName}' is refused. ") +
"redb does not store a collection directly inside a collection, nor a multi-dimensional array: the inner " +
"collections would be written as the name of their type and the object could not be loaded. Wrap the " +
"inner collection in a class, for example List with class Group { List Items { get; set; } }, " +
"or use a jagged layout of such classes for a multi-dimensional array.")
{
OwnerName = ownerName;
PropertyName = propertyName;
PropertyTypeName = typeName;
}
/// A type in C# notation: List<List<int>>, int[,].
public static string Display(Type type)
{
if (type.IsArray)
return Display(type.GetElementType()!) + "[" + new string(',', type.GetArrayRank() - 1) + "]";
if (Nullable.GetUnderlyingType(type) is { } underlying)
return Display(underlying) + "?";
if (!type.IsGenericType)
return type.Name;
var name = type.Name;
var tick = name.IndexOf('`');
if (tick >= 0)
name = name[..tick];
return name + "<" + string.Join(", ", type.GetGenericArguments().Select(Display)) + ">";
}
}