Skip to content

How to supply Values

JPVenson edited this page Aug 2, 2022 · 3 revisions

Morestachio allows to use several ways for supplying values from an object for the template. They are tried, in following order until any value is obtained:

IValueResolver

You can set an object that inherits from IValueResolver and set it with ParserOptionsBuilder.WithValueResolver(IValueResolver). The IValueResolver has two methods: CanResolve and Resolve. If you set an IValueResolver the CanResolve method is the first thing that is called for every path so ensure that your check is not too expensive. If CanResolve returns true, the Resolve method is immediately called and whatever is returned from that method call is used as the result.

If there is ether no IValueResolver or the IValueResolver.CanResolve method returns false, all checks below are executed.

ParserOptions.HandleDictionaryAsObject

Morestachio handles all instances of IDictionary<string, object> the same as an object by default so you can access all objects and all instances of IDictionary<string, object> alike. It is however possible to disable this behavior by setting ParserOptionsBuilder.WithHandleDictionaryAsObject(bool) to false.

When ParserOptionsBuilder.WithHandleDictionaryAsObject is true but the key is not present in the dictionary, the ParserOptions.OnUnresolvedPath event is triggered and null is returned.

IMorestachioPropertyResolver

If an object inherits from this interface morestachio will call the IMorestachioPropertyResolver.TryGetValue method and whatever is returned is used as a property.

When an object inherits the interface but IMorestachioPropertyResolver.TryGetValue returns false, the ParserOptions.OnUnresolvedPath event is triggered and null is returned.

ICustomTypeDescriptor

This adds explicit support for dynamic types and if present the interface will be called to obtain a property and get its value.

When no property is found in the interface, the ParserOptions.OnUnresolvedPath event is triggered and null is returned.

Reflection / FallbackValueResolver

When none of the above is in used to obtain the value, the IFallbackValueResolver is invoked. This is always set to a Reflection based resolver and will lookup the requested path via Type.GetTypeInfo and PropertyInfo.GetProperty(path). This does only lookup public properties.

When no property is found, the ParserOptions.OnUnresolvedPath event is triggered and null is returned.