tags:

views:

118

answers:

1

I'm very impressed with MonoTouch.Dialog by Miguel. inspired by his simple approach I thought it would be very nice to have a reflection based NSUserDefaults class that works seamlessly by marking certain properties as "persistable in user defaults" and forget about the rest. something like this:

    public class SomeController
    {
        [PersistInUserSettings]
        public string LastPostUserRead { get; set; }
    }

I don't like the LastPostUserRead to be of type "UserSetting", I like it to be a normal property so that later I can assign to it like:

LastPostUserRead = "Post 1";

and not like:

LastPostUserRead.SetValue("Post 1");

Any idea on how to implement it (without Reflection.Emit) in monotouch?

+1  A: 

You do not need Reflection.Emit at all to dynamically set values. That is pure System.Reflection (without the "Emit" part).

MonoTouch.Dialog in fact reads and stores values dynamically using SetValue and GetValue from the FieldInfo (could be done for properties too).

miguel.de.icaza
afaics the binding is one way (in Dialog), if I set the value of a bound field while the dialog is showing, the UI element bound to that field will not update. am I missing something?
Ali Shafai
Yes ali - you need to use bindingContext.Fetch(). See http://github.com/migueldeicaza/MonoTouch.Dialog/blob/master/Sample/DemoReflectionApi.cs for an example, I'm not sure if the wiki mentions you have to do this
Chris S