在我的应用程序中,我有使用输入和输出相互链接的组件。将要交换的数据可以是任何类型,所以我使用泛型。
public interface IInputVariable<T>
{
IOutputVariable<T> Source { get; set; }
}
public interface IOutputVariable<T>
{
T Value { get; set; }
}
在另一个类中,组件链接在一起。哪些组件被链接,是从一个文件派生的。进行链接的类不知道输入和输出交换的类型。这个类只想用下面的代码连接它们:
IOutputVariable<double> output;
Type argumentType = output.GetType().GetGenericArguments()[0];
IInputVariable<argumentType> input = new BasicInputVariable<argumentType>();
input.Source = output;
此代码无法编译,因为 argumentType 不能用作通用参数。有没有正确的方法来制定这个?或者是否可以在不知道其参数类型的情况下声明一个泛型变量?