so I'm trying to set a list<> field within an object with a new list<>.
this list could be of any type, hence the use of generics.
I get a compile time error, 'cannot convert System.Collections.Generic.List<object> expression to type System.Collections.Generic.IEnumerable<T>'
Is there anyway to make this work?
private void MyGenericMethod<T>(FieldInfo field)
{
field.SetValue(obj, new List<T>(newObjectList)); // new List<T> allObjects.ConvertAll<IEnumerable>) ???
}
I'm with Damien. The problem must be newObjectList, because there's no problem passing a List of generic type to SetValue, as it accepts two arguments of type Object
public void SetValue(
Object obj,
Object value
)
If you create a new List and populate it with another collection, it will ask for a IEnumerable, so you should try something like
field.SetValue(obj, new List<T>(newObjectList as IEnumerable<T>));
At least, at compilation time, it will not throw any error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With