Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix index access bug caused by Access Modifiers of C# class #3554

Merged
merged 2 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions libraries/AdaptiveExpressions/ExpressionFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -818,20 +818,11 @@ public static (object value, string error) AccessIndex(object instance, int inde
object value = null;
string error = null;

var count = -1;
if (TryParseList(instance, out var list))
{
count = list.Count;
}

var itype = instance.GetType();
var indexer = itype.GetProperties().Except(itype.GetDefaultMembers().OfType<PropertyInfo>());
if (count != -1 && indexer != null)
{
if (index >= 0 && count > index)
if (index >= 0 && index < list.Count)
{
dynamic idyn = instance;
value = idyn[index];
value = list[index];
}
else
{
Expand Down
17 changes: 17 additions & 0 deletions tests/AdaptiveExpressions.Tests/ExpressionParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class ExpressionParserTests

private readonly object scope = new Dictionary<string, object>
{
{
"alist", new List<A>() { new A("item1"), new A("item2") }
},
{
"emptyList", new List<object>()
},
Expand Down Expand Up @@ -260,6 +263,10 @@ public class ExpressionParserTests

public static IEnumerable<object[]> Data => new[]
{
#region accessProperty and accessIndex
Test("alist[0].Name", "item1"),
#endregion

#region string interpolation test
Test("`hi`", "hi"),
Test(@"`hi\``", "hi`"),
Expand Down Expand Up @@ -999,5 +1006,15 @@ private object ResolveValue(object obj)

return value;
}

private class A
{
public A(string name)
{
this.Name = name;
}

public string Name { get; set; }
}
}
}