To address your goal of binding the SerializationContext early and eliminating the need to pass it later when using the Definitions list, we can transform the list into one where each supplier is a parameterless factory function. This is achieved by capturing the context at the time of definition, effectively "currying" it into each supplier.
✅ Goal Recap
You have a list of definitions like this:
public static readonly List<(Type Value, byte Discriminator, Func<SerializationContext?, AwPropertyEntity> Supplier)> Definitions =
[
(typeof(AwPropertyEntity), (byte)0, context => new AwPropertyEntity(context))
];
You want to create a function that:
- Accepts a
SerializationContext
- Returns a new list of tuples where the
Supplier no longer requires a context parameter
- The
Supplier should now be a Func<AwPropertyEntity> that already uses the provided context
✅ Solution: Curried Definitions with Context Captured
Here’s how you can achieve this:
public static readonly Func<SerializationContext, List<(Type Value, byte Discriminator, Func<AwPropertyEntity> Supplier)>> CurriedDefinitions =
context => Definitions
.Select(d => (
d.Value,
d.Discriminator,
Supplier: new Func<AwPropertyEntity>(() => d.Supplier(context))
))
.ToList();
🧠 Explanation
CurriedDefinitions is now a function that takes a SerializationContext.
- For each tuple in
Definitions, it creates a new tuple:
Value and Discriminator are preserved
- The
Supplier is transformed into a parameterless function (Func<AwPropertyEntity>)
- The original
Supplier (which requires a context) is invoked immediately with the provided context, and its result is captured in a closure
✅ Usage Example
var context = new SerializationContext(...);
var curriedList = CurriedDefinitions(context);
foreach (var (Type, Discriminator, Supplier) in curriedList)
{
AwPropertyEntity entity = Supplier(); // No need to pass context here
// Do something with entity
}
This allows you to decouple the context from the usage of the supplier, making your code cleaner and more reusable.