Reputation: 21062
When you get Type of the variable you can check its name:
if (my_type.Name=="Int32")
however it would more elegant to write
if (my_type.Name==TypeNames.Int32)
to avoid typos. I can define such class on my own, but maybe there is already definition somewhere?
If yes, where?
Note: please avoid wondering "why would you like to get type of the variable in the first place", "it is better to use 'is'" and alike. Thank you very much!
Edit: meanwhile, I jumped into the conclusion it would be enough to ignore the type of the object (my_type variable) and check the object instead. In other words the my_type is not necessary. I forgot about null case :-( Less code, more sleep, that's what I need ;-)
Upvotes: 3
Views: 138
Reputation: 176169
The type names are not defined anywhere in a class. They are generated at runtime by the CLR (by a call to the external ConstructName
function) using reflection.
Using the suggestion of JaredPar will do the job.
Upvotes: 2
Reputation: 754715
Try the following
typeof(Int32).Name
If you want to compare types though doing so by name is not the best solution as it will be wrong in many cases. It's more correct to compare the types directly.
if ( m_type == typeof(Int32) ) {
...
}
Upvotes: 16