Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

reflection

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".)

like image 390
leora Avatar asked Aug 02 '10 17:08

leora


2 Answers

PropertyInfo.CanWrite (documentation)

or

PropertyInfo.GetSetMethod (documentation)

like image 161
STO Avatar answered Oct 05 '22 07:10

STO


propertyInfo.GetSetMethod() != null 
like image 28
Kirk Woll Avatar answered Oct 05 '22 07:10

Kirk Woll