missing property in dynamics treated different in asp.net?
I'm taking a look at Robs Manatee. After converting the json migrations into dynamic objects, he checks to s开发者_运维百科ee if a property exists like this:
if (col.nullable != null) {
if (col.nullable) {
sb.Append(" NULL ");
} else {
sb.Append(" NOT NULL ");
}
} else {
sb.Append(" NULL ");
}
When I try to compile this into an wpf application, I get an exception on the first line:
'System.Dynamic.ExpandoObject' does not contain a definition for 'nullable'
So, why does this work when used in a asp.net setting? Or has this something to do with WebMatrix?
The issue is that webmatrix is using it's own dynamic type rather than an expando. With an expando if a value has never been set, it will throw an exception rather than return null. I'm not sure if json fx will let you use a different "expando" like this object
.net DynamicObject implementation that returns null for missing properties rather than a RunTimeBinderException
The other option is to catch the exception for a messing member.
精彩评论