Get property value from string using reflection
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Reflection lets you inspect types and members at runtime, which is useful when the property name is only known as a string. In C#, the usual pattern is to look up a PropertyInfo, validate it, and then call GetValue on the target object.
The Basic Reflection Flow
The process has three steps:
- Get the runtime type.
- Find the property by name.
- Read the value from an object instance.
Here is the smallest useful example.
This is runnable as-is. If the property exists, the code prints Alice.
Wrapping It in a Reusable Helper
In real projects, you usually want a helper that checks for missing properties and returns a value in one place.
This is a better foundation because callers get a clear failure instead of a NullReferenceException later.
Supporting Case-Insensitive Lookup
If the property name comes from user input or external configuration, exact casing may be inconvenient. GetProperty supports binding flags for a case-insensitive lookup.
This is convenient, but use it deliberately. If the application contract says the property names are exact, silent case-insensitive lookup may hide bugs.
Nested Properties Need More Than One Lookup
A string like Address.City is not a single property. You need to resolve it segment by segment.
This pattern is common in grid components, serializers, and rule engines where property paths come from configuration.
Reflection Is Flexible, Not Free
Reflection is slower than direct property access and bypasses normal compile-time checking. That does not make it wrong, but it does mean you should keep it at boundaries where flexibility matters:
- data-binding utilities
- admin tooling
- serialization or mapping layers
- plug-in systems
If the property is known at compile time, direct access is better.
A Generic Alternative
If you know the property at compile time but still want a reusable abstraction, lambda expressions are safer than strings.
This avoids misspelled property names completely.
Common Pitfalls
The first pitfall is assuming the property always exists. GetProperty can return null, and production code should treat that as expected input, not as an impossible state.
Another mistake is ignoring non-public or static members. GetProperty without flags only looks for public instance properties. If your target member is not public, the default lookup will not find it.
Developers also forget about indexer properties. A property that expects parameters cannot be read with a simple GetValue(target) call.
Finally, reflection-heavy code often grows into a stringly typed API that is difficult to refactor. Use it where flexibility is necessary, not as a default replacement for normal object access.
Summary
- In C#, use
GetType().GetProperty(name)andGetValue(target)to read a property by string name. - Check for missing properties explicitly before reading the value.
- Use binding flags if you need case-insensitive or non-default lookup behavior.
- Resolve nested paths one segment at a time.
- Prefer direct access or lambda expressions when the property is known at compile time.

