Search code examples
c#reflectionsqlexception

C# Reflection get value from ICollection


i'm struggling with a problem and I'm unable to find a solution.

In a certain moment of my code, a SqlException is thrown.

I have created a method using reflection to iterate through all the properties from any exception and perform some logic, but I'm having an issue with this one.

One of its members call Errors, that inherits from SqlErrorCollection. (According to MSDN documentation, the class definition is

[SerializableAttribute]
[ListBindableAttribute(false)]
public sealed class SqlErrorCollection : ICollection, 

The code I use to check if a property is a list doesn't work for this property:

if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType)
    && typeof(ICollection).IsAssignableFrom(prop.PropertyType)
    && prop.PropertyType.IsGenericType)

And when I will get it value using the code below

property.GetValue(myObject, null);

an TargetParameterCountException (Parameters count mismatch) is raised.

Anyone know why?


Solution

  • Due to the insight provided by pmcoltrane, the Error property is an indexed property. The article iterating through an indexed property (Reflection) helped to solve my problem.

    Thanks for the help