How can I pass an object to a nullable of type struct?
The codebase I'm working in has a method that has a signature
Public Sub SetDropDownValue(Of T As Structure)(ByVal target As ListControl, ByVal value As Nullable(Of T))
The method I am writing is passed as a parameter of the type object.
How can I pass an object to something that can be passed to a method SetDropDownValue
?
a source to share
No, you won't be able to use a reference type as a value type (which means a restriction Structure
). The CLR allows you to inject a value type as a reference type (this is called boxing), but the nature of the difference between the implementation (and semantics) of these two different types makes the opposite impossible.
The only thing you could do is create a value type that contains a reference to your object as a field, but perhaps this problem could be a hint that you are doing this all wrong.
a source to share
This should work if you know T
:
something.SetDropDownValue(target, DirectCast(value, Nullable(Of T)))
See the article for details .
If you don't know the type T
, you're in trouble and have to start looking with reflection at runtime. It is difficult, dangerous, and has terrible performance.
a source to share