I just recently had a need in one of my projects to check if a certain type is a numeric type.
And in case you are wondering, you can use it freely and there is no reason to credit me. Just please don’t post it and say you made it…that just won’t be nice. I won’t do anything to you, so if you really don’t feel like being nice, feel free to re-post it as your own, just know it’s not nice.
The thing itself:
First of all, I made two methods to check it…I mean, I found two methods online to check it, and put them in a static class:
This is good to check if a type is numeric, but what if you wanted to use operators you can only use on numeric types, like <? In that case I needed to make a new class. I made two overloads of it.
As you can see, I have used a custom exception, here it is:
This is basically all you have to copy paste into some files in your project. I am sorry for the code not being formatted nicely…the forum just doesn’t like tabs. But you could easily fix it when you put it in your IDE.
How to use it:
Here is a good example:
I made some attribute which needs to compares things with anonymous types. So here is how I did it:
I put all the code in a spoiler thing, so it won’t take that much space. I put the spoiler on the final explanation as well because…well… I will let this explain: http://imgur.com/a/hi3fn
For anyone who is too lazy to copy-paste, you can download it here: https://drive.google.com/drive/folders/0BwN2sNbqJn9mLXNqNnFWd2dUUFk