Vixen
02b45cc4
csharp
public static class CollectionFactory

How to make a collection whose element type is only known at run time.

No guide page documents this yet — the page shows what the code says about itself.

Remarks

This exists because Array.CreateInstance(elementType, n) is the one thing a data binder needs and NativeAOT cannot do. So can MakeGenericType and Activator.CreateInstance(Type); all three are RequiresDynamicCode, and a binder built on them works on a desktop and throws on a phone. The engine found this the moment the first .meta file needed a list of per-target overrides, which is what [14](../../docs/plan/14-roadmap.md) means by scheduling Phase 3 early.

The answer is the same one the rest of this assembly gives: a generator saw the type in the source, so a generator can write the constructor. A member declared TargetOverride[] produces static count => new TargetOverride[count] — ordinary C#, statically typed, bound at compile time. Nothing at run time has to build a type.

Interfaces are registered too, backed by an array: a member declared IReadOnlyList<T> is filled with a T[], which satisfies it with no copy.

Fields and properties (1)

  • public static int Count

    How many collection types are registered.

Methods (3)

  • public static void Register(Type type, Func<int, object> create)

    Records how to make one.

  • public static bool TryCreate(Type type, int capacity, out object? instance)

    Makes one.

  • public static void Clear()

    Forgets everything. For tests that need an empty registry.

Used by (3)

  • SerializerTestsVixen.Core.Yaml.Tests
  • TypeRegistrationVixen.Animation
  • YamlSerializerVixen.Core.Yaml