© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•7mo ago•
2 replies
VoidPointer

Use Lambda or Expression To Specify Property to Get Via Reflection

I have a prototype method for fetching a named property value from a model object:
// All properties on this model are strings just to simplify this question.
public string FindProperty(Guid key, string propertyName)
{
    var dbModel = kycRepo.KycExtraInformationRepo.Get(key);
    var prop = dbModel.GetType().GetProperty(propertyName);
    return prop.GetValue(dbModel, null);
}
// All properties on this model are strings just to simplify this question.
public string FindProperty(Guid key, string propertyName)
{
    var dbModel = kycRepo.KycExtraInformationRepo.Get(key);
    var prop = dbModel.GetType().GetProperty(propertyName);
    return prop.GetValue(dbModel, null);
}

I would like to avoid the caller having to provide the property name as a string, and instead somehow use a delegate, or some sort of expression construct, to specify an actual property on
dbModel
dbModel
, where it would be called something like:
var pVal = FindProperty(Guid key, m => m.Surname)
var pVal = FindProperty(Guid key, m => m.Surname)

Then I can either use the delegate to fetch the property value, or at least reflect on the delegate to get the property name and then again use reflection to get the property value.

What ways can I go about this?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ ✅ Use Reflection or Generics to specify one or more properties
C#CC# / help
3y ago
❔ Expression<Func> using Reflection
C#CC# / help
3y ago
LINQ Lambda-Expression Diff
C#CC# / help
4y ago
✅ Delegate vs lambda function/expression
C#CC# / help
11mo ago