mardi 25 août 2020

Dynamically modifying an IEnumerable property via reflection

I have a variety of classes that have various properties that implement IEnumerable (eg IEnumerable<string>, IEnumerable<bool>, IEnumerable<enum>, etc). I'm trying to write some code to filter down the values of theses properties (eg if the value is { "one", "two", "three" } I might want to filter where .Contains("t")).

Here's the essence of what I've got:

class MyObject
{
    public IEnumerable<string> stringProp { get; set; } = new[] { "one", "two", "three" };
    public IEnumerable<bool> boolProp { get; set; } = new[] { true, false, true };
    public IEnumerable<int> intProp { get; set; } = new[] { 1, 2, 3 };
}

public static void Main(string[] args)
{
    MyObject obj = new MyObject();
    
    foreach (PropertyInfo prop in typeof(MyObject).GetProperties())
    {               
        prop.SetValue(obj, (prop.GetValue(obj) as IEnumerable<dynamic>).Where(val => val != null));
    }
}

The problem is that when I try to set the value back to the object (property.SetValue) an error is thrown because the new value is an IEnumerable<object>.

Object of type 'System.Linq.Enumerable+WhereArrayIterator`1[System.Object]' cannot be converted to type 'System.Collections.Generic.IEnumerable`1[System.String]'

I've tried Convert.ChangeType but that does not work because IEnumerable does not implement IConvertible.

How might I accomplish this? Why does the LINQ Where query change the IEnumerable<dynamic> into IEnumerable<object>?





Aucun commentaire:

Enregistrer un commentaire