Skip to content

Instantly share code, notes, and snippets.

@timgthomas
Created May 27, 2011 16:46
Show Gist options
  • Save timgthomas/995657 to your computer and use it in GitHub Desktop.
Save timgthomas/995657 to your computer and use it in GitHub Desktop.
Using GetFields() to return a comma-separated list of fields on an object.
public string GetFieldNames(Type type)
{
return string.Join(",", GetFields(type).Select(t => t.Name).ToArray());
}
public static IEnumerable<FieldInfo> GetFields(Type currentType)
{
foreach (FieldInfo fi in currentType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
if (!(typeof(Delegate)).IsAssignableFrom(fi.FieldType)) yield return fi;
}
}
// An example object
public class SampleObject
{
// Note the use of fields instead of properties.
public string Foo;
public string Bar;
public string Baz;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment