Skip to content

Commit

Permalink
Fixes arrays missing Item indexer resulting in NullReferenceException
Browse files Browse the repository at this point in the history
  • Loading branch information
zetterstrom authored Sep 18, 2020
1 parent 2fc8e1b commit b50b0ff
Showing 1 changed file with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,29 @@ private static FieldIdentifier ToFieldIdentifier(EditContext editContext, string
// This code assumes C# conventions (one indexer named Item with one param)
nextToken = nextToken.Substring(0, nextToken.Length - 1);
var prop = obj.GetType().GetProperty("Item");
var indexerType = prop.GetIndexParameters()[0].ParameterType;
var indexerValue = Convert.ChangeType(nextToken, indexerType);
newObj = prop.GetValue(obj, new object[] { indexerValue });

if(null != prop)
{
// we've got an Item property
var indexerType = prop.GetIndexParameters()[0].ParameterType;
var indexerValue = Convert.ChangeType(nextToken, indexerType);
newObj = prop.GetValue(obj, new object[] { indexerValue });
}
else
{
// If there is no Item property
// Try to cast the object to array
object[] array = obj as object[];
if (array != null)
{
int indexerValue = Convert.ToInt32(nextToken);
newObj = array[indexerValue];
}
else
{
throw new InvalidOperationException($"Could not find indexer on object of type {obj.GetType().FullName}.");
}
}
}
else
{
Expand Down

0 comments on commit b50b0ff

Please sign in to comment.