Search code examples
c#performanceoptimizationreflection

Which is the most optimal way to get or set the value of a property using reflection


What is the most optimal way to repeatedly get or set the value of a property using reflection in a long-term perspective?

I tried using plain old reflection but this solution seems a little bit slow to me. I need another solution.


Solution

  • You are correct! Reflection (in its natural form) is not a performant solution. However, I can suggest using pre-compiled expressions because they are way more efficient and by using them you can benefit from type safety. You can read more about the idea in this article on Medium.

    You can also check out a library that I contribute to - TryAtSoftware.Extensions.Reflection. It exposes extension methods that can help construct appropriate expressions. It is open-source and available as a NuGet package.