AFAIK clojure-clr doesn't have built-in support for accessing
properties. Something like this has probably been posted before, but
I've been playing with Windows.Forms, and I found the following
useful.
(import '(System Type Array Exception)
'(System.Reflection PropertyInfo MethodInfo))
(defn prop-get [object prop-name]
(let [prop-name
(if (symbol? prop-name)
(name prop-name)
prop-name)
o-type (type object)
prop (when o-type (.GetProperty o-type prop-name))
getter (when prop (.GetGetMethod prop))]
(when getter
(.Invoke getter object nil))))
(defn prop-set! [object prop-name value]
(let [prop-name
(if (symbol? prop-name)
(name prop-name)
prop-name)
o-type (type object)
v-type (type value)
prop (when o-type (.GetProperty o-type prop-name))
setter (when prop (.GetSetMethod prop))
arg (when v-type (Array/CreateInstance v-type 1))]
(when (and setter arg)
(.SetValue arg value 0)
(.Invoke setter object arg))))
;;e.g
(let [e (Exception.)]
(prop-set! e 'HelpLink "file:///foo.txt")
(prop-get e 'HelpLink))
=> "file:///foo.txt"
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en