Binding System 2
  • Welcome
  • Product
    • Binding Values
    • Minimal UI
    • Pinning Fields
    • Extending the System
      • Bind Class
      • Converters
      • Modifiers
        • List of Available Modifiers (WIP)
      • Bind Rerouting
      • Accessor Providers
    • Demo
    • Settings
    • Troubleshooting
      • Errors Visualization
      • Live Debug
      • Path Value Preview
      • Refactoring
      • Bindings Dependencies
    • Reserializer
    • Performance
    • FAQ
    • External Extensions
      • Odin Inspector
    • ‼️Upgrading from Version 1
  • Change log
    • Changes from version 1
  • Third Party Licenses
Powered by GitBook
On this page

Was this helpful?

  1. Product
  2. Extending the System

Converters

PreviousBind ClassNextModifiers

Last updated 6 months ago

Was this helpful?

Converters are used to convert one type to another type, sometimes with additional parameters. For example, a value can be converted to a string using a given format. The BindingSystem provides examples of implementing converters in the code, making it easy to understand and extend this functionality. There are essentially 2 types of Converters: Safe and Unsafe. The latter may fail during conversion and invalidate the entire data retrieval pipeline.

To add a new converter it is sufficient to implement IConverter<TFrom, TTo> interface:

public class MyConverter : IConverter<bool, string>
{
    public string Id => "My Converter";
    public string Description => "Transforms a bool into Valid or Invalid string";
    public bool IsSafe => true; // The conversion will always succeed
    public string Convert(bool value)
    {
        return value ? "Valid" : "Invalid";
    }
    public object Convert(object value) => Convert(value is bool bValue ? bValue : false);
}

Implement IContextConverter in addition to IConverter<,> if the converter needs to have information about bind source and path.

The system will automatically pick up the new converter and add it to converters list. To avoid auto registration of the new converter, add [HideMember] attribute over the class.

The converter must have a parameterless constructor.

When having multiple converters, the system will give the user the possiblity to select which converter to use.

Inspector View with a Converter