I’m trying to get all properties from a type, but using TypeDescriptor.GetProperties(thisType) will only supply me with properties, that has both setter and a getter. I have write-only properties. Are there a way to retrieve a PropertyDescriptorCollection including those?
/Asger
Write-only properties are a rare beast, and don’t exist in the System.ComponentModel / PropertyDescriptor space.
PropertyDescriptors are designed to be readable. I could probably hackHyperDescriptorto shim write-only properties, but it would be a hack – and it would presumably have to throw exceptions forget, which could impact calling code quite a bit.As an aside; I generally advise against write-only properties; the text-book example that people trot out is passwords (
public string Password {private get;set;}) – I’d much rather have avoid SetPassword(string newPassword)method…What is it that you actually want to do? There are a range of options here, all very achievable:
Delegate.CreateDelegate(very easy)Expression.Compile(a little harder, but not much)Reflection.Emit(quite hard)PropertyDescriptor(quite hard)If you let me know what you actually want to do (rather than the way you are currently trying to do it), I might be able to help more.
As an example using
Delegate.CreateDelegate(note you would want to stash the delegate somewhere and re-use it lots of times):edited to show how to do it if you don’t know the specific types at runtime
Or alternatively using the
ExpressionAPI (.NET 3.5):