I'm trying to test if a property exists on my document type:
Model.PageTitle.IsNull
And this works great if the property PageTitle does not exist. However, if it does, it will be cast to string and I get and error ('string' does not contain a definition for 'IsNull').
So how am I supposed to prevent errors from the property not existing, without getting errors when the property DOES exist?
I've also tried HasValue, but apparently: 'string' does not contain a definition for 'HasValue'
They are most likely getters instead of methods, which means my first post was wrong.
I will need to implement IsNull and HasValue on DynamicNode as well as DynamicNull, to allow you to use it regardless of if you get a DynamicNull or not.
This is a really quick patch, in fact, i'll go get my laptop and do it and commit it in the next 30 min, so you can retest. Don't know when the final is coming out and would like this one fixed
I just realised, if you have a native type, e.g. string, i'm going to have to put them on there as extension methods - to get the seemless test like you're after..
Shouldn't be too difficult, there's only about 6 or 7 possible types at the moment
Ah yes, that works fine, that's a good check as well. But I would like it if HasValue would check if the prop exists AND has a value, as you will want to do that a lot of the time (a bit like string.IsNullOrEmpty).
How do I use IsNull / HasValue
I'm trying to test if a property exists on my document type:
And this works great if the property PageTitle does not exist. However, if it does, it will be cast to string and I get and error ('string' does not contain a definition for 'IsNull').
So how am I supposed to prevent errors from the property not existing, without getting errors when the property DOES exist?
I've also tried HasValue, but apparently: 'string' does not contain a definition for 'HasValue'
In 4.7rc, use HasProperty; so: Model.HasProperty("PageTitle")
In 4.7 [final], when a property doesn't exist, DynamicNull will be returned, so you can use one of these three:
1) Model.someProperty.GetType() == typeof(DynamicNull)
2) Model.someProperty.IsNull() [true]
3) Model.someProperty.HasValue() [false]
2 & 3 That doesn't seem to work as expected:
Gives me false - which is correct
Then
Gives an error: Cannot invoke a non-delegate type
Also, when the property does exist:
(also tried with HasValue() )
Gives me the error: 'string' does not contain a definition for 'HasValue'
The same behaviour occurs when using IsNull.
The only thing that works reliably is using the first method (GetType()).
So right now HasValue and IsNull can only be used when you're sure that the property does NOT exist, which is backwards.. ;-)
I was using the RC: 1.0.4087.21620 - changeset: 7f84eb55e573
Just to make sure, I upgraded to the latest changeset: 1.0.4089.21758 - changeset: c2a5ffd2c4e7
But the behaviour is the same still.
They are most likely getters instead of methods, which means my first post was wrong.
I will need to implement IsNull and HasValue on DynamicNode as well as DynamicNull, to allow you to use it regardless of if you get a DynamicNull or not.
This is a really quick patch, in fact, i'll go get my laptop and do it and commit it in the next 30 min, so you can retest. Don't know when the final is coming out and would like this one fixed
Excellent! Final coming out in about 12-24 hours according to Niels on Twitter just now! :)
I just realised, if you have a native type, e.g. string, i'm going to have to put them on there as extension methods - to get the seemless test like you're after..
Shouldn't be too difficult, there's only about 6 or 7 possible types at the moment
Did .HasProperty(string) work for you ?
Ah yes, that works fine, that's a good check as well. But I would like it if HasValue would check if the prop exists AND has a value, as you will want to do that a lot of the time (a bit like string.IsNullOrEmpty).
Have been playing around, don't think it's going to be possible
I can't get the extension methods to be available on the types (string, etc) from razor - (and i had to change them to be methods not getters)
It's easy enough to add them to my own types (DynamicNode, DynamicMedia etc) but when you have an actual property, it's going to be a native type
Shame! But .HasProperty(string) works great, so I'll stick with that. It's much easier to read and understand than the GetType() version anyway.
is working on a reply...