What is the best way to extract any fields or properties from a .NET class that are single-valued (i.e. not an array, collection, etc.)
So far I have
[...]
FieldInfo[] fieldInfos = foo.GetType().GetFields(bindingFlags);
fieldInfos = fieldInfos.Where(f => !f.FieldType.IsArrayOrCollection()).ToArray();
[...]
and this in a static extensions class
public static bool IsArrayOrCollection(this Type testType)
{
if (testType.IsArray)
return true;
if (testType is IEnumerable)
return true;
return false;
}
It works but I wonder if there is a better way? Are there any corner cases this will miss?
The rationale for this is that I am storing single-valued members in a database table as key-value pairs. Clearly this won't work for arrays as the value would be a nested type and needs storing in a separate table of its own.
Aucun commentaire:
Enregistrer un commentaire