从泛型 (C#) 创建强类型对象的编译错误
Compile error creating strongly typed object from generic (C#)
这是我的 类...除了用于说明问题区域的最低限度代码外,我已经删除了所有内容。下面,我用代码注释标记了未编译的行(它们在 Mapping.GetMapTuples())
public class MappingSource<T>
{
protected Dictionary<string, T> dictMap = new Dictionary<string, T>();
public MappingSource()
{
}
public T GetValue(string key)
{
T value;
if (dictMap.TryGetValue(key, out value))
{
return value;
}
return default(T);
}
}
public class Mapping<S, D>
{
MappingSource<S> _source1;
MappingSource<D> _source2;
List<Tuple<string, string>> tuples;
public Mapping(MappingSource<S> source1, MappingSource<D> source2)
{
tuples = new List<Tuple<string, string>>();
_source1 = source1;
_source2 = source2;
}
public List<MapTuple<S, D>> GetMapTuples<S, D>()
{
List<MapTuple<S, D>> list = new List<MapTuple<S, D>>();
foreach (Tuple<string, string> tuple in tuples)
{
// here I need to be able to return a list of MapTuple
// objects. I tried three variations; none of them work;
// this way doesn't work
// "Cannot implicitly convert 'D' to 'D[C:\test\Form1.cs(166)]'
S vs = _source1.GetValue(tuple.Item1);
D vd = _source2.GetValue(tuple.Item2);
MapTuple<S, D> mapTuple = new MapTuple<S, D>(vs, vd);
// this way doesn't work; it doesn't like the type var
// The best overloaded method match for 'Examples16.MapTuple<S,D>.MapTuple(S,D)' has some invalid arguments
var vsv = _source1.GetValue(tuple.Item1);
var vdv = _source2.GetValue(tuple.Item2);
MapTuple<S, D> mapTuple2 = new MapTuple<S, D>(vsv, vdv);
// this way doesn't work; same error as the first way
// "Cannot implicitly convert 'D' to 'D[C:\test\Form1.cs(166)]'
var vsv2 = _source1.GetValue(tuple.Item1);
var vdv2 = _source2.GetValue(tuple.Item2);
MapTuple<S, D> mapTuple3 = new MapTuple<S, D>((S)vsv2, (D)vdv2);
}
return null;
}
}
public class MapTuple<S, D>
{
S _source;
D _destination;
public MapTuple(S source, D destination)
{
_source = source;
_destination = destination;
}
}
唯一需要的是:
public List<MapTuple<S, D>> GetMapTuples()
请注意,该函数不再有类型参数。我假设编译器已经警告过你,这些 S 和 D 基本上 shadow 是为 class 定义的外部的。这是我从编译器收到的消息:
Type parameter 'S' has the same name as the type parameter from outer type 'Mapping<S, D>'
因此这里定义S
和D
:
MappingSource<S> _source1;
MappingSource<D> _source2;
不是此处定义的(并在方法内部使用):
GetMapTuples<S, D>()
看看fiddle。
这是我的 类...除了用于说明问题区域的最低限度代码外,我已经删除了所有内容。下面,我用代码注释标记了未编译的行(它们在 Mapping.GetMapTuples())
public class MappingSource<T>
{
protected Dictionary<string, T> dictMap = new Dictionary<string, T>();
public MappingSource()
{
}
public T GetValue(string key)
{
T value;
if (dictMap.TryGetValue(key, out value))
{
return value;
}
return default(T);
}
}
public class Mapping<S, D>
{
MappingSource<S> _source1;
MappingSource<D> _source2;
List<Tuple<string, string>> tuples;
public Mapping(MappingSource<S> source1, MappingSource<D> source2)
{
tuples = new List<Tuple<string, string>>();
_source1 = source1;
_source2 = source2;
}
public List<MapTuple<S, D>> GetMapTuples<S, D>()
{
List<MapTuple<S, D>> list = new List<MapTuple<S, D>>();
foreach (Tuple<string, string> tuple in tuples)
{
// here I need to be able to return a list of MapTuple
// objects. I tried three variations; none of them work;
// this way doesn't work
// "Cannot implicitly convert 'D' to 'D[C:\test\Form1.cs(166)]'
S vs = _source1.GetValue(tuple.Item1);
D vd = _source2.GetValue(tuple.Item2);
MapTuple<S, D> mapTuple = new MapTuple<S, D>(vs, vd);
// this way doesn't work; it doesn't like the type var
// The best overloaded method match for 'Examples16.MapTuple<S,D>.MapTuple(S,D)' has some invalid arguments
var vsv = _source1.GetValue(tuple.Item1);
var vdv = _source2.GetValue(tuple.Item2);
MapTuple<S, D> mapTuple2 = new MapTuple<S, D>(vsv, vdv);
// this way doesn't work; same error as the first way
// "Cannot implicitly convert 'D' to 'D[C:\test\Form1.cs(166)]'
var vsv2 = _source1.GetValue(tuple.Item1);
var vdv2 = _source2.GetValue(tuple.Item2);
MapTuple<S, D> mapTuple3 = new MapTuple<S, D>((S)vsv2, (D)vdv2);
}
return null;
}
}
public class MapTuple<S, D>
{
S _source;
D _destination;
public MapTuple(S source, D destination)
{
_source = source;
_destination = destination;
}
}
唯一需要的是:
public List<MapTuple<S, D>> GetMapTuples()
请注意,该函数不再有类型参数。我假设编译器已经警告过你,这些 S 和 D 基本上 shadow 是为 class 定义的外部的。这是我从编译器收到的消息:
Type parameter 'S' has the same name as the type parameter from outer type 'Mapping<S, D>'
因此这里定义S
和D
:
MappingSource<S> _source1;
MappingSource<D> _source2;
不是此处定义的(并在方法内部使用):
GetMapTuples<S, D>()
看看fiddle。