private NameValueCollection Convert(object value)
        {
            if (value is NameValueCollection)
            {
                var collection = new NameValueCollection();
                var source     = (NameValueCollection)value;
                foreach (string key in source.AllKeys)
                {
                    collection.Add(key, source[key]);
                }
                return(collection);
            }

            if (value is IDictionary)
            {
                var collection = new NameValueCollection();
                var source     = (IDictionary)value;
                var typeMapper = NativeTypeMapper.GetTypeMapper <string>();
                foreach (DictionaryEntry keyVal in source)
                {
                    collection.Add(typeMapper.Cast(keyVal.Key), typeMapper.Cast(keyVal.Value));
                }
                return(collection);
            }

            return(null);
        }
        private T[] Convert(object value)
        {
            var type = typeof(T);

            if (type.IsPrimitive || type.IsValueType)
            {
                if (value is ICollection <T> )
                {
                    var source = (ICollection <T>)value;
                    var array  = new T[source.Count];
                    source.CopyTo(array, 0);
                    return(array);
                }
                if (value is IEnumerable <T> )
                {
                    var list = new List <T>();
                    foreach (T obj in (IEnumerable <T>)value)
                    {
                        list.Add(obj);
                    }
                    return(list.ToArray());
                }
            }
            if (value is IEnumerable)
            {
                var list       = new List <T>();
                var typeMapper = NativeTypeMapper.GetTypeMapper <T>();
                foreach (object obj in (IEnumerable)value)
                {
                    list.Add(typeMapper.Cast(obj));
                }
                return(list.ToArray());
            }
            return(null);
        }
        private T Convert <TElementType>(IEnumerable source)
        {
            ICollection <TElementType> dest = (ICollection <TElementType>)Create();
            var typeMapper = NativeTypeMapper.GetTypeMapper <TElementType>();

            foreach (object element in source)
            {
                dest.Add(typeMapper.Cast(element));
            }
            return((T)dest);
        }
예제 #4
0
        private T Convert <TElementType>(IEnumerable source)
        {
            var dest       = (ISet <TElementType>)Create();
            var typeMapper = NativeTypeMapper.GetTypeMapper <TElementType>();

            foreach (var element in source)
            {
                dest.Add(typeMapper.Cast(element));
            }

            return((T)dest);
        }
        protected virtual T Convert <TKey, TValue, TSourceKey, TSourceValue>(IDictionary <TSourceKey, TSourceValue> source)
        {
            var dictionary = (IDictionary <TKey, TValue>)Create();

            var typeMapper  = NativeTypeMapper.GetTypeMapper <TKey>();
            var typeMapper2 = NativeTypeMapper.GetTypeMapper <TValue>();

            foreach (KeyValuePair <TSourceKey, TSourceValue> keyVal in source)
            {
                dictionary.Add(typeMapper.Cast(keyVal.Key), typeMapper2.Cast(keyVal.Value));
            }

            return((T)dictionary);
        }
예제 #6
0
 private ReadOnlyCollection <TElementType> Convert(object value)
 {
     if (value is IEnumerable)
     {
         var collection = new Collection <TElementType>();
         var typeMapper = NativeTypeMapper.GetTypeMapper <TElementType>();
         foreach (var obj in (IEnumerable)value)
         {
             collection.Add(typeMapper.Cast(obj));
         }
         return(new ReadOnlyCollection <TElementType>(collection));
     }
     return(null);
 }
        protected virtual T Convert(IDictionary source)
        {
            var dictionary = (IDictionary)Create();

            foreach (DictionaryEntry keyVal in source)
            {
                var type = keyVal.Key.GetType();
                if (keyVal.Value == null)
                {
                    dictionary.Add(NativeTypeMapper.GetTypeMapper(type).Cast(keyVal.Key), null);
                }
                else
                {
                    var type2 = keyVal.Value.GetType();
                    dictionary.Add(NativeTypeMapper.GetTypeMapper(type).Cast(keyVal.Key), NativeTypeMapper.GetTypeMapper(type2).Cast(keyVal.Value));
                }
            }

            return((T)dictionary);
        }
        private ArrayList Convert(object value)
        {
            if (value is IEnumerable)
            {
                ArrayList list;
                if (value is ICollection)
                {
                    var source = (ICollection)value;
                    list = new ArrayList(source.Count);
                }
                else
                {
                    list = new ArrayList();
                }

                foreach (var obj in (IEnumerable)value)
                {
                    if (obj == null)
                    {
                        list.Add(null);
                    }
                    else
                    {
                        var type = obj.GetType();
                        if (type.IsPrimitive || type.IsValueType)
                        {
                            list.Add(obj);
                        }
                        else
                        {
                            list.Add(NativeTypeMapper.GetTypeMapper(obj.GetType()).Cast(obj));
                        }
                    }
                }
                return(list);
            }
            return(null);
        }
 private T Convert(object value)
 {
     if (value is IEnumerable)
     {
         if (genericCollectionConvert != null)
         {
             return(genericCollectionConvert((IEnumerable)value));
         }
         else
         {
             var collection = Create();
             var list       = (IList)collection;
             foreach (var obj in (IEnumerable)value)
             {
                 if (obj == null)
                 {
                     list.Add(null);
                 }
                 else
                 {
                     var type = obj.GetType();
                     if (type.IsPrimitive || type.IsValueType)
                     {
                         list.Add(obj);
                     }
                     else
                     {
                         list.Add(NativeTypeMapper.GetTypeMapper(type).Cast(obj));
                     }
                 }
             }
             return((T)collection);
         }
     }
     return(default(T));
 }