From b63d1065d3d7694f8cdc40d689ad5e756026d145 Mon Sep 17 00:00:00 2001 From: maurok Date: Fri, 19 May 2017 15:41:40 -0300 Subject: [PATCH] # Adding support for array types within actions --- .../LuisActionResolver.cs | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/CSharp/Blog-LUISActionBinding/Microsoft.Cognitive.LUIS.ActionBinding/LuisActionResolver.cs b/CSharp/Blog-LUISActionBinding/Microsoft.Cognitive.LUIS.ActionBinding/LuisActionResolver.cs index b037d46eaf..b998a5494e 100644 --- a/CSharp/Blog-LUISActionBinding/Microsoft.Cognitive.LUIS.ActionBinding/LuisActionResolver.cs +++ b/CSharp/Blog-LUISActionBinding/Microsoft.Cognitive.LUIS.ActionBinding/LuisActionResolver.cs @@ -348,7 +348,11 @@ private static bool AssignValue(ILuisAction action, PropertyInfo property, objec try { - if (type.IsEnum) + if (type.IsArray) + { + property.SetValue(action, BuildArrayOfValues(action, property, type.GetElementType(), paramValue)); + } + else if (type.IsEnum) { property.SetValue(action, Enum.Parse(type, (string)paramValue)); } @@ -369,6 +373,35 @@ private static bool AssignValue(ILuisAction action, PropertyInfo property, objec return false; } + private static Array BuildArrayOfValues(ILuisAction action, PropertyInfo property, Type elementType, object paramValue) + { + var values = default(IEnumerable); + if (paramValue is IEnumerable) + { + values = paramValue as IEnumerable; + } + else + { + values = paramValue.ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(v => v.Trim()); + } + + if (values.Count() > 0) + { + var idx = 0; + var result = Array.CreateInstance(elementType, values.Count()); + foreach (var value in values) + { + result.SetValue(elementType.IsEnum ? Enum.Parse(elementType, (string)value) : Convert.ChangeType(value, elementType), idx++); + } + + return result; + } + else + { + return null; + } + } + private static bool AssignEntitiesToMembers( ILuisAction action, IEnumerable properties,