Skip to content

Commit 0487d53

Browse files
author
Jicheng Lu
committed
refine side car attribute
1 parent a20eacb commit 0487d53

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/Infrastructure/BotSharp.Abstraction/SideCar/Attributes/SideCarAttribute.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ private static MethodInfo GetMethod(string name)
6262
try
6363
{
6464
var sidecar = serviceProvider.GetService<IConversationSideCar>();
65-
var argTypes = args.Select(x => x.GetType()).ToArray();
65+
var argTypes = args.Select(x => x != null ? x.GetType() : null).ToArray();
6666
var sidecarMethod = sidecar?.GetType()?.GetMethods(BindingFlags.Public | BindingFlags.Instance)
6767
.FirstOrDefault(x => x.Name == methodName
6868
&& x.ReturnType == retType
6969
&& x.GetParameters().Length == argTypes.Length
7070
&& x.GetParameters().Select(p => p.ParameterType)
71-
.Zip(argTypes, (paramType, argType) => paramType.IsAssignableFrom(argType)).All(y => y));
71+
.Zip(argTypes, (paramType, argType) => IsParameterTypeMatch(paramType, argType)).All(y => y));
7272

7373
return (sidecar, sidecarMethod);
7474
}
@@ -78,6 +78,28 @@ private static MethodInfo GetMethod(string name)
7878
}
7979
}
8080

81+
private static bool IsParameterTypeMatch(Type paramType, Type? argType)
82+
{
83+
// If argument is null, check if parameter type is nullable
84+
if (argType == null)
85+
{
86+
// Check if it's a nullable value type (e.g., int?)
87+
if (paramType.IsGenericType && paramType.GetGenericTypeDefinition() == typeof(Nullable<>))
88+
{
89+
return true;
90+
}
91+
// Check if it's a reference type (which are inherently nullable)
92+
if (!paramType.IsValueType)
93+
{
94+
return true;
95+
}
96+
return false;
97+
}
98+
99+
// Normal type matching
100+
return paramType.IsAssignableFrom(argType);
101+
}
102+
81103
private async Task<(bool, object?)> CallAsyncMethod(IConversationSideCar instance, MethodInfo method, Type retType, object[] args)
82104
{
83105
object? value = null;

src/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ limitations under the License.
1515
******************************************************************************/
1616

1717
using BotSharp.Abstraction.Repositories.Filters;
18-
using BotSharp.Abstraction.SideCar.Options;
1918
using BotSharp.Core.Infrastructures;
2019

2120
namespace BotSharp.Core.SideCar.Services;

0 commit comments

Comments
 (0)