Using reflection, how do I detect properties that have setters?

C#Reflection

C# Problem Overview


I have this code to loop through an object and get all of its properties through reflection:

foreach (var propertyInfo in typeof(TBase).GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
    var oldValue = propertyInfo.GetValue(oldVersion, null);
}

How can I do a check to only look at properties that have a "Set" on them? (I want to ignore read-only values - just "Get".)

C# Solutions


Solution 1 - C#

PropertyInfo.CanWrite (documentation)

or

PropertyInfo.GetSetMethod (documentation)

Solution 2 - C#

propertyInfo.GetSetMethod() != null

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionleoraView Question on Stackoverflow
Solution 1 - C#STOView Answer on Stackoverflow
Solution 2 - C#Kirk WollView Answer on Stackoverflow