Skip to content

Commit

Permalink
Merge pull request microsoft#132 from southworkscom/csharp-luis-actio…
Browse files Browse the repository at this point in the history
…ns-arraysupport

[CSharp-LUISActionBinding] Support for Array types within actions
  • Loading branch information
willportnoy authored Jun 23, 2017
2 parents 11347cc + b63d106 commit e09abd0
Showing 1 changed file with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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<object>);
if (paramValue is IEnumerable<object>)
{
values = paramValue as IEnumerable<object>;
}
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<PropertyInfo> properties,
Expand Down

0 comments on commit e09abd0

Please sign in to comment.