I have a class with properties on it. We will call this TestMeCommand (see below). This class has a list on it. What I need to do is loop over the properties of the class, and identify the List. Now this has to be built generically because its code for validation, so this same code might need to identify a List<int> or a List<string>, or something else.
public class TestMeCommand
{
[Range(1, Int32.MaxValue)]
public int TheInt { get; set; }
[Required]
[StringLength(50)]
public string TheString { get; set; }
[ListNotEmptyValidator]
public List<TestListItem> MyList { get; set; }
public class TestListItem
{
[Range(1, Int32.MaxValue)]
public int ListInt { get; set; }
}
}
Now the problem is that I have code that looks like this:
foreach (var prop in this.GetType().GetProperties())
{
if (prop.PropertyType.FullName.StartsWith("System.Collections.Generic.List"))
{
IList list = prop.GetGetMethod().Invoke(this, null) as IList;
}
}
I dont want to put that string in there, but if I do something like prop.PropertyType is IList it never evaluates true. How do I fix it?
I might use:
which covers anything implementing
IList.The reason that
prop.PropertyType is IListnever evaluates true is that this is asking “does myTypeobject implementIList?”, rather than “does the type represented by thisTypeobject implementIList?”.