AIFunctionFactoryOptions.MarshalResult Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets a delegate used to determine the Object returned by InvokeAsync(AIFunctionArguments, CancellationToken).
public:
property Func<System::Object ^, Type ^, System::Threading::CancellationToken, System::Threading::Tasks::ValueTask<System::Object ^>> ^ MarshalResult { Func<System::Object ^, Type ^, System::Threading::CancellationToken, System::Threading::Tasks::ValueTask<System::Object ^>> ^ get(); void set(Func<System::Object ^, Type ^, System::Threading::CancellationToken, System::Threading::Tasks::ValueTask<System::Object ^>> ^ value); };
public Func<object?,Type?,System.Threading.CancellationToken,System.Threading.Tasks.ValueTask<object?>>? MarshalResult { get; set; }
member this.MarshalResult : Func<obj, Type, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask<obj>> with get, set
Public Property MarshalResult As Func(Of Object, Type, CancellationToken, ValueTask(Of Object))
Property Value
Remarks
By default, the return value of invoking the method wrapped into an AIFunction by AIFunctionFactory is then JSON serialized, with the resulting JsonElement returned from the InvokeAsync(AIFunctionArguments, CancellationToken) method. This default behavior is ideal for the common case where the result will be passed back to an AI service. However, if the caller requires more control over the result's marshaling, the MarshalResult property may be set to a delegate that is then provided with complete control over the result's marshaling. The delegate is invoked with the value returned by the method, and its return value is then returned from the InvokeAsync(AIFunctionArguments, CancellationToken) method.
When set, the delegate is invoked even for void
-returning methods, in which case it is invoked with a null
argument. By default, null
is returned from the InvokeAsync(AIFunctionArguments, CancellationToken) method for AIFunction instances produced by AIFunctionFactory to wrap void
-returning methods).
Methods strongly-typed to return types of Task, Task<TResult>, ValueTask, and ValueTask<TResult> are special-cased. For methods typed to return Task or ValueTask, MarshalResult will be invoked with the null
value after the returned task has successfully completed. For methods typed to return Task<TResult> or ValueTask<TResult>, the delegate will be invoked with the task's result value after the task has successfully completed.These behaviors keep synchronous and asynchronous methods consistent.
In addition to the returned value, which is provided to the delegate as the first argument, the delegate is also provided with a Type represented the declared return type of the method. This can be used to determine how to marshal the result. This may be different than the actual type of the object (GetType()) if the method returns a derived type or null
. If the method is typed to return Task, ValueTask, or void
, the Type argument will be null
.