diff --git a/src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactory.cs b/src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactory.cs
index 6534e041a7c..d5274186645 100644
--- a/src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactory.cs
+++ b/src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactory.cs
@@ -7,7 +7,6 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
-using System.Diagnostics.CodeAnalysis;
using System.IO;
#if !NET
using System.Linq;
@@ -374,16 +373,15 @@ public static AIFunction Create(MethodInfo method, object? target, string? name
}
///
- /// Creates an instance for a method, specified via an for
- /// and instance method, along with a representing the type of the target object to
- /// instantiate each time the method is invoked.
+ /// Creates an instance for a method, specified via a for
+ /// an instance method and a for constructing an instance of
+ /// the receiver object each time the is invoked.
///
/// The instance method to be represented via the created .
- ///
- /// The to construct an instance of on which to invoke when
- /// the resulting is invoked. is used,
- /// utilizing the type's public parameterless constructor. If an instance can't be constructed, an exception is
- /// thrown during the function's invocation.
+ ///
+ /// Callback used on each function invocation to create an instance of the type on which the instance method
+ /// will be invoked. If the returned instance is or , it will be disposed of
+ /// after completes its invocation.
///
/// Metadata to use to override defaults inferred from .
/// The created for invoking .
@@ -457,22 +455,16 @@ public static AIFunction Create(MethodInfo method, object? target, string? name
///
///
/// is .
- /// is .
+ /// is .
/// represents a static method.
/// represents an open generic method.
/// contains a parameter without a parameter name.
- /// is not assignable to 's declaring type.
/// A parameter to or its return type is not serializable.
public static AIFunction Create(
MethodInfo method,
- [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type targetType,
- AIFunctionFactoryOptions? options = null)
- {
- _ = Throw.IfNull(method);
- _ = Throw.IfNull(targetType);
-
- return ReflectionAIFunction.Build(method, targetType, options ?? _defaultOptions);
- }
+ Func createInstanceFunc,
+ AIFunctionFactoryOptions? options = null) =>
+ ReflectionAIFunction.Build(method, createInstanceFunc, options ?? _defaultOptions);
private sealed class ReflectionAIFunction : AIFunction
{
@@ -503,10 +495,11 @@ public static ReflectionAIFunction Build(MethodInfo method, object? target, AIFu
public static ReflectionAIFunction Build(
MethodInfo method,
- [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type targetType,
+ Func createInstanceFunc,
AIFunctionFactoryOptions options)
{
_ = Throw.IfNull(method);
+ _ = Throw.IfNull(createInstanceFunc);
if (method.ContainsGenericParameters)
{
@@ -518,13 +511,7 @@ public static ReflectionAIFunction Build(
Throw.ArgumentException(nameof(method), "The method must be an instance method.");
}
- if (method.DeclaringType is { } declaringType &&
- !declaringType.IsAssignableFrom(targetType))
- {
- Throw.ArgumentException(nameof(targetType), "The target type must be assignable to the method's declaring type.");
- }
-
- return new(ReflectionAIFunctionDescriptor.GetOrCreate(method, options), targetType, options);
+ return new(ReflectionAIFunctionDescriptor.GetOrCreate(method, options), createInstanceFunc, options);
}
private ReflectionAIFunction(ReflectionAIFunctionDescriptor functionDescriptor, object? target, AIFunctionFactoryOptions options)
@@ -536,20 +523,17 @@ private ReflectionAIFunction(ReflectionAIFunctionDescriptor functionDescriptor,
private ReflectionAIFunction(
ReflectionAIFunctionDescriptor functionDescriptor,
- [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type targetType,
+ Func createInstanceFunc,
AIFunctionFactoryOptions options)
{
FunctionDescriptor = functionDescriptor;
- TargetType = targetType;
- CreateInstance = options.CreateInstance;
+ CreateInstanceFunc = createInstanceFunc;
AdditionalProperties = options.AdditionalProperties ?? EmptyReadOnlyDictionary.Instance;
}
public ReflectionAIFunctionDescriptor FunctionDescriptor { get; }
public object? Target { get; }
- [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
- public Type? TargetType { get; }
- public Func? CreateInstance { get; }
+ public Func? CreateInstanceFunc { get; }
public override IReadOnlyDictionary AdditionalProperties { get; }
public override string Name => FunctionDescriptor.Name;
@@ -566,14 +550,12 @@ private ReflectionAIFunction(
object? target = Target;
try
{
- if (TargetType is { } targetType)
+ if (CreateInstanceFunc is { } func)
{
Debug.Assert(target is null, "Expected target to be null when we have a non-null target type");
Debug.Assert(!FunctionDescriptor.Method.IsStatic, "Expected an instance method");
- target = CreateInstance is not null ?
- CreateInstance(targetType, arguments) :
- Activator.CreateInstance(targetType);
+ target = func(arguments);
if (target is null)
{
Throw.InvalidOperationException("Unable to create an instance of the target type.");
diff --git a/src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactoryOptions.cs b/src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactoryOptions.cs
index 80ff394359d..e71a4687422 100644
--- a/src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactoryOptions.cs
+++ b/src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactoryOptions.cs
@@ -106,24 +106,6 @@ public AIFunctionFactoryOptions()
///
public Func