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

Exception in ContextualType with System.Collection.ArrayList #155

Open
diabhoil opened this issue Jul 16, 2024 · 1 comment
Open

Exception in ContextualType with System.Collection.ArrayList #155

diabhoil opened this issue Jul 16, 2024 · 1 comment

Comments

@diabhoil
Copy link

I am getting an "Failed to retrieve enumerable item type of System.Collection.ArrayList" exception with inner exception "InvalidOperationException: Sequence contains more than one matching element" at

var getEnumeratorMethod = Methods.SingleOrDefault(m => m.Name == "GetEnumerator");

Error occured in NJsonSchema but is thrown in Namotion.Reflection.

Using .Net 8

@pickarjt
Copy link

pickarjt commented Aug 21, 2024

I was able to fix this problem locally by updating ContextualType.cs replacing the below statement:

var getEnumeratorMethod = Methods.SingleOrDefault(m => m.Name == "GetEnumerator");

with this:

// Ensure class implements IEnumerable since it is possible for a class to contain a GetEnumerator() method
// even if the class doesn't implement IEnumerable.
if (!TypeInfo.ImplementedInterfaces.Any(i => "System.Collections.IEnumerable".Equals(i.FullName, StringComparison.Ordinal)))
{
	return null;
}

// If a class defines more than one GetEnumerator() method (System.Collections.ArrayList, eg), 
// then find the GetEnumerator() method that takes the least number of input arguments -
// note, the GetEnumerator() method defined by IEnumerable takes zero input arguments.
//
// EndsWith(".GetEnumerator") check is done in case a GetEnumerator() method is namespaced;
// for example: IEnumerator IEnumerable.GetEnumerator().
ContextualMethodInfo getEnumeratorMethod = Methods
	.Where(m => m.Name.Equals("GetEnumerator", StringComparison.Ordinal) || m.Name.EndsWith(".GetEnumerator", StringComparison.Ordinal))
	.OrderBy(m => m.Parameters.Length)
	.FirstOrDefault();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants