确定派生自哪个通用类型对象
我有以下类:
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)
{
}
}
如何确定对象的类型是 Command
或 Command
?我不知道 TArgumentType 或 TReturnType 是什么具体类型。或者我应该做一个简单的尝试/捕获:
var returnValue = object.ReturnValue;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在编译时不知道类型,则
foo.ReturnValue
甚至无法编译,除非它是dynamic
类型。您可以使用这样的东西:
像这样调用它:
注意,这不会用于查找已实现的接口,这有点棘手。
If you don't know the type at compile-time, then
foo.ReturnValue
won'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.