Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 索引器类和自动映射器C_C# 4.0_Automapper - Fatal编程技术网

C# 4.0 索引器类和自动映射器C

C# 4.0 索引器类和自动映射器C,c#-4.0,automapper,C# 4.0,Automapper,如何通过AutoMapper映射2索引器类?我需要映射两个具有属性use CollectionItem类型的模型。我试着用AutoMapper。但它不起作用。请参见下面我的示例索引器类: public class CollectionItem { private readonly IEnumerable<string> _keys; private readonly IDictionary<string, IList<Item>> _relat

如何通过AutoMapper映射2索引器类?我需要映射两个具有属性use CollectionItem类型的模型。我试着用AutoMapper。但它不起作用。请参见下面我的示例索引器类:

public class CollectionItem
{
    private readonly IEnumerable<string> _keys;
    private readonly IDictionary<string, IList<Item>> _relatedContents;
    private static readonly IList<Item> _emptyList = new List<Item>();

    public CollectionItem(IEnumerable<string> keys)
    {
        _keys = keys;
        _relatedContents = new Dictionary<string, IList<Item>>();
    }

    public IList<Item> this[string key]
    {
        get
        {
            if (!ContainsKey(key))
            {
                throw new KeyNotFoundException("The given key was not present in the dictionary");
            }
            return _relatedContents.ContainsKey(key) ? _relatedContents[key] : _emptyList;
        }
    }

    public bool ContainsKey(string key)
    {
        return !string.IsNullOrEmpty(key) && _keys.Contains(key, StringComparer.OrdinalIgnoreCase);
    }
}

谢谢

您可以编写自己的自定义类型解析器:

class FromCollection
{
    private List<string> _items;

    public int Count 
    {
        get { return _items.Count; }
    }
    public string this[int index]
    {
        get { return _items[index]; }
        set { 
            _items[index] = value; 
        }
    }

    public FromCollection()
    {
        _items = new List<string>(Enumerable.Repeat("", 10));
    }
}

class ToCollection
{
    private List<string> _items;

    public string this[int index]
    {
        get { return _items[index]; }
        set { 
            _items[index] = value; 
        }
    }

    public ToCollection()
    {
        _items = new List<string>(Enumerable.Repeat("", 10));
    }
}

class IndexerTypeConverter : TypeConverter<FromCollection, ToCollection>
{
    protected override ToCollection ConvertCore(FromCollection source)
    {
        ToCollection result = new ToCollection();

        for (int i = 0; i < source.Count; i++)
        {
            result[i] = source[i];   
        }

        return result;
    }
}

internal class Program
{
    private static void Main(string[] args)
    {
        Mapper.CreateMap<FromCollection, ToCollection>()
            .ConvertUsing<IndexerTypeConverter>();

        var src = new FromCollection();
        src[3] = "hola!";

        var dst = Mapper.Map<ToCollection>(src);
        Console.WriteLine();
    }
}
您是如何尝试使用AutoMapper的?它是如何不起作用的?