Given the following method:
public dynamic ConvertIt(dynamic source, Type dest)
{
return Convert.ChangeType(source, dest);
}
How can I get the actual primitive type of dest?
I would expect something like :
if (dest is bool)
However I get a design time warning that the type condition will never meet, which is obvious because dest is always Type.
I would expect to be able to get to an enum of types (primitives), a property of dest.
You can use the typeof
operator with your known type and compare that with the dest
type:
if (dest == typeof(bool))
(Reference equality is fine here, as each type only has a single Type
object representing it.)
It's not clear what you mean by "get to an enum of types (primitives), a property of dest" - but if you want to check whether dest
is one of a bunch of types, you could create a List<Type>
or HashSet<Type>
and just use acceptableTypes.Contains(dest)
.
See more on this question at Stackoverflow