I have a C# wraper class with a series of methods accepting various data types:
public class MyClass
{
public void ProcessString(string Value) { // implementation }
public void ProcessInt(int? Value) { // implementation }\
public void ProcessOther(MyClass Value) { // implementation }
}
I now want to add a generic ProcessObject()
method to avoid the need to explicitly cast an object before calling a relevant process method:
public void ProcessObject(object Value)
{
if (CanCastToString(Value)
{
ProcessString((string)Value);
}
else if (CanCastToInt(Value))
{
ProcessInt((int?)Value);
}
// etc...
}
The trouble is that I don't know what my CanCastToInt
methods should be - I need these methods to be able to be robust and deal with things like nullable types and other user defined casts.
How can I do this? All I want to know is if a given object can be cast to a given type, i.e. whether or not:
(SomeType)Value
Will work.