I have the following classes:
public abstract class CommandBase
{
... Stuff
}
public abstract class Command<TArgumentType>
: CommandBase where TArgumentType : class
{
protected TArgumentType Argument { get; private set; }
protected Command(TArgumentType argument)
{
Argument = argument;
}
}
public abstract class Command<TArgumentType, TReturnType>
: Command<TArgumentType> where TArgumentType : class
{
public TReturnType ReturnValue{ get; protected set; }
protected Command(TArgumentType argument) : base(argument)
{
}
}
How do I determine if an object is of type Command<TArgumentType> or Command<TArgumentType, TReturnType>? I don’t know what specific types TArgumentType or TReturnType are. Or should I just do a simple try/catch around:
var returnValue = object.ReturnValue;
If you don’t know the type at compile-time, then
foo.ReturnValuewon’t even compile, unless it’s of typedynamic.You can use something like this:
Call it like this:
Note that this won’t work for finding implemented interfaces, which is somewhat trickier.