HOW TO: Set Properties Dynamically Using XAF/XPO

XPO provides some very powerful features which are wrapped up again in a nice library for XAF. One such feature is the System.Reflection metadata that is wrapped into a nice library that allows you to find attributes easier etc. I recently came across a situation which required me to set an unknown property with an unknown value and show any errors if the typing didn’t work or the property wasn’t found. To do this, I used the XafTypes Instance of XAF to find the type and member info. The code looked something like this:

Dim dtiTypeInfo As TypeInfo
dtiTypeInfo = XafTypesInfo.Instance.FindTypeInfo(GetType(ObjectType))
If dtiTypeInfo Is Nothing Then
    Return Nothing
End If
Return dtiTypeInfo.FindMember(MyFieldName)

The next challenge I ran into was deserializing the values of the object, which again XAF and XPO provide a nice wrapper to forget about all of the noise. I simply used the deserialize method of the member info that I just returned and passed in the string value that I wanted to set. This automatically converted everything to what I need and also allows you to serialize down properties into databases if you wanted to and set properties based on their deserialized value. This was a one liner that looks like this:

MyMemberInfo.SetValue(vrtVariant,MyMemberInfo.DeserializeValue(Value.ToString))

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>