Skip to content

Instantly share code, notes, and snippets.

@MirzaLeka
Last active July 23, 2024 16:27
Show Gist options
  • Save MirzaLeka/b7716b1f8f8d9076d7aa6c940c5765be to your computer and use it in GitHub Desktop.
Save MirzaLeka/b7716b1f8f8d9076d7aa6c940c5765be to your computer and use it in GitHub Desktop.
Object.keys & Object.values in C#

C# - Read Object MetaData using Reflections

C# is statically typed language which requires explicit handling for runtime inspection.

Reflection is what allows C# developers to inspect data structures by looking up:

  • The type of an object
  • Properties
  • Methods, etc.
var user = new UserData()
{
    Id = 1,
    Name = "Mirza",
    Age = 30
};

var keys = user.GetType().GetProperties().Select(prop => prop.Name).ToList(); // [Id, Name, Age]
var values = user.GetType().GetProperties().Select(prop => prop.GetValue(user, null)).ToList(); // [1, Mirza, 30]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment