Skip to content

Commit

Permalink
Add failed type to exception message in EnumerableItemType, RicoSuter…
Browse files Browse the repository at this point in the history
  • Loading branch information
Rico Suter committed Oct 6, 2023
1 parent 7152297 commit 38b43c2
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions src/Namotion.Reflection/Context/ContextualType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,34 +137,41 @@ public ContextualType? EnumerableItemType
{
get
{
var elementType = ElementType;
if (elementType != null)
try
{
return elementType;
}

var getEnumeratorMethod = Methods.SingleOrDefault(m => m.Name == "GetEnumerator");
if (getEnumeratorMethod != null)
{
if (GenericArguments?.Length == 1)
var elementType = ElementType;
if (elementType != null)
{
return GenericArguments[0];
return elementType;
}

if (_enumerableItemType != null)
var getEnumeratorMethod = Methods.SingleOrDefault(m => m.Name == "GetEnumerator");
if (getEnumeratorMethod != null)
{
return _enumerableItemType;
}
if (GenericArguments?.Length == 1)
{
return GenericArguments[0];
}

var returnParam = getEnumeratorMethod.ReturnParameter;
if (returnParam?.GenericArguments.Length == 1)
{
_enumerableItemType = returnParam.GenericArguments[0];
return _enumerableItemType;
if (_enumerableItemType != null)
{
return _enumerableItemType;
}

var returnParam = getEnumeratorMethod.ReturnParameter;
if (returnParam?.GenericArguments.Length == 1)
{
_enumerableItemType = returnParam.GenericArguments[0];
return _enumerableItemType;
}
}
}

return null;
return null;
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to retrieve enumerable item type of {Type.FullName}.", exception);
}
}
}

Expand Down

0 comments on commit 38b43c2

Please sign in to comment.