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.
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.
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.
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.
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.Staticand only searching instance members. - Passing an object instance to
GetValuefor a static property instead ofnull. - Using
GetPropertywhen 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
GetPropertywithBindingFlags.Staticto find a static property. - Add
BindingFlags.PublicorBindingFlags.NonPublicdepending on visibility. - Call
GetValue(null)andSetValue(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
- How to Get a Sublist in C
- How to get ALL child controls of a Windows Forms form of a specific type Button/Textbox?
- How to get around the memory leak in the .NET Webbrowser control?
- How to get awaitable Thread.Sleep?
- How to get awaitable Thread.Sleep?
- How to get csc.exe path?
- How to get first object out from ListObject using Linq
- How to get index using LINQ?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.