Register custom translator
Every translator must implement the
GenericMethodCompiler exposed by DotVVM.
Example 1: translator for
In the following code snippet, we will create a custom translation for the method
Where(source, predicate) on the type
System.Linq.Enumerable. Whenever constructing the
var translator = new GenericMethodCompiler(args => args.Member("filter").Invoke(args));
In the code snippet above,
args represents the source collection, and
At this point, you might notice that we are indexing the
args array from 1. Actually, the zero-th array element represents the
this pointer. Since in C# the
Where method is static, the zero-th array element is
null, and as such is not used by the translator. The only step left is to register the new translator. This can be achieved in
DotvvmStartup.cs file using the following code snippet.
Example 2: Translator for
Array.Length property getter method
In the following code snippet, we will create a custom translation for the
Length property getter method on the type
System.Array. In this case, we need to work with a single arguments that represents the array. Since the
Length property is bound to a specific instance, the array argument is passed as the
this pointer, which corresponds to the
args expression. An example translation can be seen in the code snippet below.
var translator = new GenericMethodCompiler(a => a.Member("length"));
args.length. Now, the only thing left is registering this translation. We can perform the registration using the following code snippet.
AddPropertyGetterTranslator(typeof(Array), nameof(Array.Length), translator);