通过反射动态修改 IEnumerable 属性
Dynamically modifying an IEnumerable property via reflection
我有多种 类,它们具有实现 IEnumerable
的各种属性(例如 IEnumerable<string>
、IEnumerable<bool>
、IEnumerable<enum>
等)。我正在尝试编写一些代码来过滤这些属性的值(例如,如果值为 { "one", "two", "three" }
我可能想在 .Contains("t")
的位置过滤)。
这是我得到的精华:
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));
}
}
问题是当我尝试将值设置回对象 (property.SetValue
) 时抛出错误,因为新值是 IEnumerable<object>
.
Object of type 'System.Linq.Enumerable+WhereArrayIterator`1[System.Object]' cannot be converted to type 'System.Collections.Generic.IEnumerable`1[System.String]'
我试过 Convert.ChangeType
但这不起作用,因为 IEnumerable
没有实现 IConvertible
.
我怎样才能做到这一点?为什么 LINQ Where
查询将 IEnumerable<dynamic>
更改为 IEnumerable<object>
?
我的理解正确吗?您在找这样的东西吗?
var obj = new MyObject();
foreach (var prop in typeof(MyObject).GetProperties())
{
//assumming all things are IEnumerable<something>
var type = prop.PropertyType.GenericTypeArguments[0];
//We can't "instantiate" something as ephemeral as an IEnumerable,
//so we need something more concrete like a List
//There might be other ways to filter - this seemed to be the easiest
var listType = typeof(List<>).MakeGenericType(type);
var instance = (IList)Activator.CreateInstance(listType);
var currentEnum = (IEnumerable)prop.GetValue(obj);
foreach (var item in currentEnum)
{
if (item != default) // != null would be silly for booleans and ints
{
instance.Add(item);
}
}
prop.SetValue(obj, instance);
}
总结:泛型和动态关键字通常不会以这种方式混合 - 拥有动态泛型参数毫无意义。将动态视为实际上意味着“对象”的东西,但也可以让你写任何你喜欢的东西。当然,IEnumerable
我有多种 类,它们具有实现 IEnumerable
的各种属性(例如 IEnumerable<string>
、IEnumerable<bool>
、IEnumerable<enum>
等)。我正在尝试编写一些代码来过滤这些属性的值(例如,如果值为 { "one", "two", "three" }
我可能想在 .Contains("t")
的位置过滤)。
这是我得到的精华:
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));
}
}
问题是当我尝试将值设置回对象 (property.SetValue
) 时抛出错误,因为新值是 IEnumerable<object>
.
Object of type 'System.Linq.Enumerable+WhereArrayIterator`1[System.Object]' cannot be converted to type 'System.Collections.Generic.IEnumerable`1[System.String]'
我试过 Convert.ChangeType
但这不起作用,因为 IEnumerable
没有实现 IConvertible
.
我怎样才能做到这一点?为什么 LINQ Where
查询将 IEnumerable<dynamic>
更改为 IEnumerable<object>
?
我的理解正确吗?您在找这样的东西吗?
var obj = new MyObject();
foreach (var prop in typeof(MyObject).GetProperties())
{
//assumming all things are IEnumerable<something>
var type = prop.PropertyType.GenericTypeArguments[0];
//We can't "instantiate" something as ephemeral as an IEnumerable,
//so we need something more concrete like a List
//There might be other ways to filter - this seemed to be the easiest
var listType = typeof(List<>).MakeGenericType(type);
var instance = (IList)Activator.CreateInstance(listType);
var currentEnum = (IEnumerable)prop.GetValue(obj);
foreach (var item in currentEnum)
{
if (item != default) // != null would be silly for booleans and ints
{
instance.Add(item);
}
}
prop.SetValue(obj, instance);
}
总结:泛型和动态关键字通常不会以这种方式混合 - 拥有动态泛型参数毫无意义。将动态视为实际上意味着“对象”的东西,但也可以让你写任何你喜欢的东西。当然,IEnumerable