In the following code I want to return an IEnumerable without creating a new data structure object. However, I get a compiler error with the following code. What am I missing?
Error Cannot implicitly convert type 'System.Reflection.FieldInfo[]' to 'System.Reflection.FieldInfo'
public static IEnumerable<FieldInfo> GetAllFields(Type objectType)
{
while (objectType != null)
{
//GetFields(...) returns a FieldInfo []
yield return objectType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
objectType = objectType.BaseType;
}
}
Type.GetFields returns an array of FieldInfo objects.
Since you´re not returning a collection of arrays, you have to iterate over the array and yield return each object in it, kinda like this: