dimanche 11 octobre 2015

TargetInvocationException @ PropertyInfo.GetValue(target, null) with Target Type MemoryStream

I wanted to read all the public property values of objects and wrote following code:

private List<PropertyInfo> GetListOfProperties(object objectToRegister,
                                BindingFlags bindingFlags = BindingFlags.Instance |
                                                            BindingFlags.Public)
{
    Type type = objectToRegister.GetType();
    List<PropertyInfo> curListOfProperties = new List<PropertyInfo>();
    curListOfProperties.AddRange(type.GetProperties()
                                    .Where((propertyInfo) =>
                                    !propertyInfo.GetIndexParameters().Any()));
    return curListOfProperties;
}

And call it like this:

var objectToRegister = new MemoryStream();

// ... write things into MemoryStream ....
foreach (var propertyInfo in GetListOfProperties(objectToRegister))
{
    if (propertyInfo.CanRead)
    {
        // -->> TargetInvocationException
        value = propertyInfo.GetValue(declaringInstance, null); 
    }
}

The Exception looks like this

System.InvalidOperationException: Timeouts are not supported on this stream. at System.IO.Stream.get_ReadTimeout()

Now I would like to exclude such unsupported Properties from the return value of GetListOfProperties





Aucun commentaire:

Enregistrer un commentaire