コード例 #1
0
ファイル: DictionaryMapper.cs プロジェクト: saber-wang/nlite
        public override void Map(ref object from, ref object to)
        {
            if (from == null)
            {
                return;
            }
            var sourceEnumerableValue = (IEnumerable)from;

            var sourceInfo = new DictionaryInfo();
            var destInfo   = new DictionaryInfo();

            GetDictionaryInfo(fromType, ref sourceInfo);
            GetDictionaryInfo(toType, ref destInfo);

            if (to == null)
            {
                if (destInfo.IsGeneric)
                {
                    to = ObjectCreator.Create(typeof(Dictionary <,>).MakeGenericType(destInfo.Key, destInfo.Value));
                }
                else
                {
                    to = ObjectCreator.Create(destInfo.Type);
                }
            }

            var addMethod = destInfo.Type.GetMethod("Add", new Type[] { destInfo.Key, destInfo.Value }).GetProc();
            var getKey    = sourceInfo.Kvp.GetProperty("Key").GetGetter();
            var getValue  = sourceInfo.Kvp.GetProperty("Value").GetGetter();

            var kvpEquals = sourceInfo.Kvp == destInfo.Kvp;
            var keyEquals = kvpEquals ? true
                : (sourceInfo.Key == destInfo.Key ||
                   destInfo.Key.IsAssignableFrom(sourceInfo.Key));

            var valueEquals = kvpEquals ? true
                : (sourceInfo.Value == destInfo.Value ||
                   destInfo.Value.IsAssignableFrom(sourceInfo.Value));

            foreach (var item in sourceEnumerableValue)
            {
                var key   = getKey(item);
                var value = getValue(item);


                var dstKey =
                    keyEquals
                    ?
                    key
                    : Mapper.Map(key, Types.Object, destInfo.Key);

                var dstValue =
                    valueEquals
                    ?
                    value
                    :
                    Mapper.Map(value, Types.Object, destInfo.Value);

                addMethod(to, dstKey, dstValue);
            }
        }
コード例 #2
0
ファイル: CollectionMapper.cs プロジェクト: saber-wang/nlite
 protected override IList CreateTarget(Type targetType, Type targetElementType, int length)
 {
     return(ObjectCreator.CreateList(targetType, targetElementType, length));
 }