简体   繁体   中英

Compile error creating strongly typed object from generic (C#)

Here are my classes...I have cut out everything but the bare minumum of code to illustrate the problem area. Below, I have marked the lines which are not compiling with code comments (they are in 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;
    }
}

The only thing required is this:

public List<MapTuple<S, D>> GetMapTuples()

Notice there is no type params for the function anymore. And I assume compiler has already warned you that these S and D basically shadow the outer ones defined for the class. Here is the message I received from the compiler:

Type parameter 'S' has the same name as the type parameter from outer type 'Mapping<S, D>'

Therefore S and D defined here:

MappingSource<S> _source1;
MappingSource<D> _source2;

are not the ones defined here (and used inside the method):

GetMapTuples<S, D>()

Have a look at the fiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM