Andreas Zita
Andreas Zita

Reputation: 7580

Get property type of dynamic object in C# (even when value is null)

I have a DynamicObject wrapper that is basically forwarding property values from another private object instance (amongst other things). But is there any way of returning the property-type dynamically as well?

The reason I'm asking is because I'm having trouble with bindings in WPF trying to convert the view-value (string in my case with a textbox bound to a double property) to a presenter value of type object instead of double (which it would if the object was not dynamic and the property had a returntype of double).

If there where something like TryGetMemberType it would be great...

Upvotes: 1

Views: 770

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063774

No that is not possible with dynamic: a null has no type, and dynamic has no metadata.

In most UI data-binding code, though (including WPF) the primary API is System.ComponentModel - which supports a form of dynamic behavior (and has done since 1.0). Typically, you would use ICustomTypeDescriptor, TypeDescriptionProvider or ITypedList to provide custom PropertyDescriptor instances. These have full metadata, including type information.

I wonder whether this would be a better fit for your needs.

Upvotes: 1

Related Questions