On Jan 15, 2013, at 9:57 PM, CreMean <[email protected]> wrote:
> Here is the code I use to get a setter via reflection:
Remember, there are plenty of properties which have no setters. That said...
> // Call
> var mySetter = GetPropertySetter<Func<int>>(myObject, "X");
That doesn't look right at all. Property setters (almost?) always have a `void`
return type, so you should be using Action<...>, not Func<...>. Furthermore,
assuming that `myObject.X` is an instance method, the delegate should have a
first parameter which matches myObject.GetType()... (See below.)
> The problem is, if myObject is the a ".Net" object, it works fine. But if the
> object is a "Java.Lang.Object", the GetSetMethod() will always return null.
You will need to provide more context, as It Works For Me™:
class Foo : Java.Lang.Object {
public int Bar {get; set;}
}
static class Demo {
public static void Run ()
{
var f = new Foo ();
var d = GetPropertySetter<Action<Foo, int>> (f, "Bar");
d (f, 42);
Console.WriteLine ("f.Bar={0}", f.Bar);
}
public static TDelegate GetPropertySetter<TDelegate> (System.Object o,
string propName)
{
var prop = o.GetType ().GetProperty (propName);
var setter = prop.GetSetMethod ();
return (TDelegate) (object) Delegate.CreateDelegate
(typeof(TDelegate), setter);
}
}
Is this a Debug app? A Release app? Do you have Link All Assemblies enabled on
a Release app and are you only accessing the property setter through Reflection?
- Jon
_______________________________________________
Monodroid mailing list
[email protected]
UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid