Trying to write a utilty method which determines whether a type is parseable (i.e. has a method like: Parse(string value)
The code below works, but seems a bit kludgey:
public static bool IsParseable(Type t) {
string s = "foo";
Type[] typeArray = { s.GetType() };
if (t.GetRuntimeMethod("Parse", typeArray )==null) {
return false;
} else {
return true;
}
}
It seems like there should be a better way to get my hands on String type then having to create an instance of the type (string) in order to call GetType()
This likewise comes up trying to use the method, as in:
bool results = IsParseable(Double); //doesn't compile.
Instead to find out if double is parseable, I have to do something like.
Double v = 1.0;
Type doubleType = v.GetType();
bool result = IsParseable(doubleType);
Is there a more efficient way? Ultimately I want to use this with generic types, where I have a type parameter T and I want to find out if T has a Parse(String value) method:
IsParseable(T)
which of course doesn't work either. And creating an instance of T isn't a great solution because not known if T has a default constructor.