我有一个泛型方法,在T类型的基础上,我需要调用更严格的泛型方法,如何在没有反射的情况下做到这一点?在示例中,我使用 as 显然是错误的
public T foo1<T>()
{
if(T is IMyInterface)
{
return SpecificMethod<T as IMyInterface>();
}
}
private T SpecificMethod<T>() where T : IMyInterface
{
// IMyInterface specific implementation
}
private T GenericMethod<T>()
{
// something generic to do
}
答案 0 :(得分:0)
您可以通过以下方式进行处理:
public interface IMyInterface { }
public T Foo1<T>()
{
if (typeof(T) == typeof(IMyInterface))
{
return (T)SpecificMethod<IMyInterface>();
}
return default(T);
}
private T SpecificMethod<T>() where T : IMyInterface
{
return default(T);
}
private T GenericMethod<T>()
{
return default(T);
}
您要的内容无法完成。