I have an object/class, and I would like to get a list/array of any public List<> members/fields only for it. And at a later point set those members.
currently I can get all the public fields with :
var fieldValues = obj[index].GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);
and a bit of research says I can test GetGenericTypeDefinition() == typeof(List<>), tho I can't test this directly against the values returned in fieldValues.
So in summation, I want to get all the public fields for an object. Sort out the List<>, and set those members.
Thanks for any help provided!
How about something like this,
var fieldValues = test.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);
foreach(var fieldValue in fieldValues)
{
if (fieldValue.FieldType.IsGenericType && fieldValue.FieldType.GetGenericTypeDefinition() == typeof(List<>))
{
fieldValue.SetValue(test, new List<string>()
{
"List Item 1",
"List Item 2"
});
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.