You can just use Linq's Select
extension method to get just the names:
IEnumerable<string> GetAllFieldNames(Type type)
{
// uses your existing method
return GetAllFields(type).Select(f => f.Name);
}
Or the ToDictionary
extension method to construct a dictionary:
IDictionary<string, object> GetAllFieldNamesAndValues(object instance)
{
return instance.GetType()
.GetFields()
.ToDictionary(f => f.Name, f => f.GetValue(instance));
}
Note you will need an instance of the type to get the values. Also, this will only work for a single type, since you will need an instance of each type to get the values.
However, if you defined your fields as static you could do this:
class AirCraft
{
public class fighterJets
{
public static string forSeas = "fj_f18";
public static string ForLand = "fj_f15";
}
public class helicopters
{
public static string openFields = "Apachi";
public static string CloseCombat = "Cobra";
}
}
IEnumerable<FieldInfo> GetAllStaticFields(Type type)
{
return type.GetNestedTypes().SelectMany(GetAllFields)
.Concat(type.GetFields(BindingFlags.Public | BindingFlags.Static));
}
IDictionary<string, object> GetAllStaticFieldNamesAndValues(Type type)
{
return GetAllStaticFields(type)
.ToDictionary(f => f.Name, f => f.GetValue(null));
}
This works because static fields are not bound to any instance of the class.