So I have the following code:
所以我有以下代码:
public Dictionary<int, Class1<Class2>> someDictionary;
public T DoThings<T, U>(int id) where T : Class1<U>, new() where U : Class2, new()
{
T something = null;
if (!someDictionary.ContainsKey(id))
{
something = new T();
someDictionary.Add(id, something);
}
return something;
}
And I am getting this error while trying to add to someDictionary:
我在尝试添加到someDictionary时收到此错误:
Argument 2: Cannot convert from 'T' to 'Class1 < Class2 >'
参数2:无法从'T'转换为'Class1
'
Any help as to how to solve this, if it even can be solved would be greatly appriciated, thanks.
关于如何解决这个问题的任何帮助,如果它甚至可以解决,将会非常感激,谢谢。
Luke.
卢克。
1 个解决方案
#1
0
You should declare the generic class.
您应该声明泛型类。
public class ShouldOneClass<T,U> where T : Class1<U>, new()
where U : Class2, new()
{
public Dictionary<int, T> someDictionary;
}
But if you don't want use generic class , replace generics type with interface.
但是如果您不想使用泛型类,请将泛型类型替换为接口。
#1
0
You should declare the generic class.
您应该声明泛型类。
public class ShouldOneClass<T,U> where T : Class1<U>, new()
where U : Class2, new()
{
public Dictionary<int, T> someDictionary;
}
But if you don't want use generic class , replace generics type with interface.
但是如果您不想使用泛型类,请将泛型类型替换为接口。