🧰Accessor Providers

This section is reserved for experienced developers

Accessor providers are reserved for more experienced users but offer greater flexibility when extending the system. They provide a way to supply custom values and paths for bindings.

By default the system will use highly optimized reflection and methods generation to provide binding paths and values. There is, however, another way to provide bindings: IAccessorProvider. All classes implementing this interface will be automatically registered by the system and offered to users as alternative binding methods.

Here is a sample implementation of IAccessorProvider interface:

    public class MyProvider : IAccessorProvider
    {
        public string Id => "Texture Data Provider";

        public IAccessor GetAccessor(Type sourceType, string pathId)
        {
            return pathId switch
            {
                "Width" => new ObjectAccessor<Texture2D, int>(t => ((Texture2D)t).width, null),
                "Height" => new ObjectAccessor<Texture2D, int>(t => ((Texture2D)t).height, null),
                _ => throw new ArgumentException(nameof(pathId)),
            };
        }

        public IEnumerable<AccessorPath> GetAvailablePaths(object source)
        {
            if(source is not Texture2D)
            {
                return Enumerable.Empty<AccessorPath>();
            }
            return new AccessorPath[]
            {
                new AccessorPath(this, "Width", BindMode.Read, typeof(int)),
                new AccessorPath(this, "Height", BindMode.Read, typeof(int)),
            };
        }

        public bool TryConvertIdToPath(string id, string separator, out string path)
        {
            path = id;
            return true;
        }
    }

The system will automatically register this new provider and here is what the user will be able to do:

Last updated