Reflection
Static Property
Programming
C#
.NET

How to get a Static property with Reflection

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In C#, a static property belongs to the type itself, not to an instance. Reflection can access it easily once you ask for the right PropertyInfo and remember that GetValue and SetValue use null for the target object when the property is static.

Get the property with the right binding flags

The most common mistake is calling GetProperty without flags that include static members. Reflection defaults are often narrower than people expect, so be explicit.

csharp
1using System;
2using System.Reflection;
3
4public class AppSettings
5{
6    public static string EnvironmentName { get; set; } = "Development";
7}
8
9public static class Program
10{
11    public static void Main()
12    {
13        Type type = typeof(AppSettings);
14        PropertyInfo? property = type.GetProperty(
15            "EnvironmentName",
16            BindingFlags.Public | BindingFlags.Static
17        );
18
19        if (property is null)
20        {
21            throw new InvalidOperationException("Property not found.");
22        }
23
24        object? value = property.GetValue(null);
25        Console.WriteLine(value);
26    }
27}

The important line is BindingFlags.Public | BindingFlags.Static. Without Static, reflection will search instance properties instead.

Read and write static property values

Once you have PropertyInfo, getting and setting a static property is straightforward. The target object is null because no instance is involved.

csharp
1using System;
2using System.Reflection;
3
4public class FeatureFlags
5{
6    public static bool Enabled { get; set; } = false;
7}
8
9public static class Program
10{
11    public static void Main()
12    {
13        PropertyInfo? property = typeof(FeatureFlags).GetProperty(
14            "Enabled",
15            BindingFlags.Public | BindingFlags.Static
16        );
17
18        if (property is null)
19        {
20            throw new InvalidOperationException("Property not found.");
21        }
22
23        property.SetValue(null, true);
24        bool value = (bool)property.GetValue(null)!;
25
26        Console.WriteLine(value);
27    }
28}

This is also the pattern to use when the type is not known at compile time and comes from a loaded assembly or configuration string.

Non-public static properties

If the property is private, protected, or internal, include BindingFlags.NonPublic as well. Reflection can then find the member if the runtime allows access.

csharp
1PropertyInfo? secretProperty = typeof(AppSettings).GetProperty(
2    "SecretToken",
3    BindingFlags.Static | BindingFlags.NonPublic
4);

That said, private reflection should be a deliberate choice. It is useful in frameworks, test tooling, and migration code, but it couples your code tightly to implementation details.

Static property versus static field

Another common source of confusion is asking reflection for a property when the member is actually a field. Properties use GetProperty; fields use GetField. Auto-properties compile down to backing fields internally, but reflection still treats the public surface as a property.

If the lookup returns null, verify the member kind first before assuming the flags are wrong. It is also worth checking whether the property is declared on a base type and whether your flags need FlattenHierarchy for inherited public and protected static members.

Cache reflection results when repeated

If this lookup happens once during startup, reflection overhead is irrelevant. If it happens inside a frequently executed path, cache the PropertyInfo or build a delegate once and reuse it. Reflection is flexible, but repeated lookup by name is slower than direct access and slower than a cached handle.

Common Pitfalls

  • Forgetting BindingFlags.Static and only searching instance members.
  • Passing an object instance to GetValue for a static property instead of null.
  • Using GetProperty when the member is actually a field.
  • Looking only for public members when the property is non-public.
  • Relying heavily on reflection in hot paths where cached delegates or direct access would be simpler and faster.

Summary

  • Use GetProperty with BindingFlags.Static to find a static property.
  • Add BindingFlags.Public or BindingFlags.NonPublic depending on visibility.
  • Call GetValue(null) and SetValue(null, value) because static properties do not need an instance.
  • Confirm that the member is really a property and not a field.
  • Reflection is powerful, but it is best used deliberately rather than as a default access pattern.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.