我想用通用参数构造一个通用类而不知道类型。
public void AddGenericValue<T>(T t1, T t2)
{
List<T> genericList = new List<T>();
genericList.Add(new GenericClass(t1, t2));
}
public class GenericClass<T>
{
T t1;
T t2;
public GenericClass(T t1, T t2)
{
this.t1 = t1;
this.t2 = t2;
}
}
现在我得到了错误使用通用类型'GenericClass'需要1种参数。
这可以解决您的问题:
在声明类型时,需要指定通用参数:
同样,列表应该是正确的类型: