I have the following class definition:
[ActionsClass[typeof(MyActions)]
public class NkProject
{
int ProjectId;
IList<NkItem> Items;
NkStory SingleItem;
}
The class mostly contains only fields, with the class attribute stating which class is responsible for "handling" this class as part of a larger framework.
Using reflection, I parse this file to generate some meta data and I'd like to know if "Items" is of type List (or any type of collection which acts as a list) but everything I've tried so far fails:
-
Check for
IEnumerable
:if(field.FieldType.GetInterface("IEnumerable") != null)
This also detects strings as IEnumerables, which is incorrect. I'd prefer if strings fail to pass this check and get treated as primitives in a sense (the
else
of the aboveif
). -
Changed above to:
if(field is ICollection)
and that also fails. -
Changed above to:
if(field is IList && field.GetType().IsGenericType)
and it still fails to be detected correctly.
Is there a way to look only at the type of the variable references to ascertain whether Items
is of type List and a parallel check for string Items
fails? I'm guessing #2 and #3 above fail because Items
is not an object and thus there isn't any inheritance information attached to it. I could be wrong, but I think that's what is happening. A field.GetType().Name
returns the type as IList`1. Not sure how to get around this.
Aucun commentaire:
Enregistrer un commentaire