The Windows Runtime provides a class named PropertyValue
which is a helper class for boxing non-inspectables. “Boxing” means taking a value type and wrapping inside an object so it can be used as an object type, and in the Windows Runtime, “objects” are represented by the IInspectable
interface.
There are a variety of static methods of the form PropertyValue.
, like CreateInt32()
or CreateDateTimeArray()
. These take values and wrap them inside an object that implements the IPropertyValue
interface,¹ and then you can use the Type
property to see the type of the value hiding inside, and the corresponding GetSomething()
method to retrieve the value.
One of the static methods is PropertyValue.
. What does this even mean? Does it wrap an IInspectable
inside another PropertyValue
, which is itself an IInspectable
?
No. There is no wrapping of IInspectable
s.
The CreateInspectable()
method merely returns its non-null parameter² unchanged. It doesn’t return a wrapper.
This means that if your original object does not implement IPropertyValue
(and there’s no reason to expect it to), then the object that comes out of PropertyValue.
is not an IPropertyValue
. In practice, it means that no IPropertyValue
s will ever return PropertyType::
. That enum field is just a mirage.³
So why does PropertyValue.
even exist?
I’m not sure. Perhaps it was added for completeness.
¹ The object also implements the corresponding IReference<T>
interface, where T
is the wrapped value.
² If the parameter is null, then it fails with an invalid argument exception.
³ I guess you could use it in your own code to mean that “The thing you have is already an IInspectable
, not a PropertyValue
wrapper around a value type.”
PropertyType WhatIsThisThing(IInspectable const& thing) { if (auto propertyValue = thing.try_as<IPropertyValue>()) { return propertyValue.Type(); } return PropertyType::Inspectable; }
0 comments
Be the first to start the discussion.