Member-only story

😎 The power of C# reflection

How many of those C# reflection tricks do you know?

Mina PĂȘcheux
CodeX

--

Photo by Tim Stief on Unsplash

Reflection is an incredible tool that allows you to interact with your object types at runtime. Be it to create an object with a dynamically computed type, or to get the type of variable that is not knowable beforehand by the programmer, it is a very powerful feature that lets you write highly abstract code.

In a nutshell, reflection is a way of working with types that are not known at compile-time but at runtime.

So, today, let’s go over some util snippets using reflection in C# to see how powerful it can be!

Note: by the way, I’ve had the chance to show various use cases for C# reflection in my tutorial series on how to make a RTS game in Unity/C#, so if you want “real-life” scenarios of using this tool in dev, make sure to check it out :)

Snippet #1: Listing all the fields and properties of a class type

When you want to learn more about a class, reflection can tell you very useful things like the fields or properties it contains. This can be really cool for automating handmade serialisation or displaying basic info in an agnostic way, for example.

--

--

Responses (1)