예제 #1
0
        public static object ConvertFromInterLinqGrouping <TKey, TElement>(Type wantedType, InterLinqGrouping <TKey, TElement> grouping)
        {
#if !NETFX_CORE
            Type[] genericArguments = wantedType.GetGenericArguments();
#else
            Type[] genericArguments = wantedType.GetTypeInfo().GenericTypeArguments;
#endif
            object key = ConvertFromSerializable(genericArguments[0], grouping.Key);

#if !NETFX_CORE
            MethodInfo method = typeof(TypeConverter).GetMethod("ConvertFromSerializableCollection", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(genericArguments[1]);
#else
            MethodInfo method = typeof(TypeConverter).GetTypeInfo().GetDeclaredMethod("ConvertFromSerializableCollection").MakeGenericMethod(genericArguments[1]);
#endif
            object elements = method.Invoke(null, new object[] { grouping });

            //object elements = ConvertFromSerializableCollection<TElement>( typeof( IEnumerable<> ).MakeGenericType( genericArguments[1] ),  );
            Type elementType = InterLinqTypeSystem.FindIEnumerable(elements.GetType());
            if (elementType == null)
            {
                throw new Exception("ElementType could not be found.");
            }
#if !NETFX_CORE
            Type[] genericTypes = new[] { key.GetType(), elementType.GetGenericArguments()[0] };
#else
            Type[] genericTypes = new[] { key.GetType(), elementType.GetTypeInfo().GenericTypeArguments[0] };
#endif
            InterLinqGroupingBase newGrouping = (InterLinqGroupingBase)Activator.CreateInstance(typeof(InterLinqGrouping <,>).MakeGenericType(genericTypes));
            newGrouping.SetKey(key);
            newGrouping.SetElements(elements);
            return(newGrouping);
        }
예제 #2
0
        private static object ConvertToInterLinqGrouping <TKey, TElement>(IGrouping <TKey, TElement> grouping)
        {
            object key         = ConvertToSerializable(grouping.Key);
            object elements    = ConvertToSerializableCollection <TElement>(grouping);
            Type   elementType = InterLinqTypeSystem.FindIEnumerable(elements.GetType());

            if (elementType == null)
            {
                throw new Exception("ElementType could not be found.");
            }
            Type[] genericTypes = new Type[] { key.GetType(), elementType.GetGenericArguments()[0] };
            InterLinqGroupingBase newGrouping = (InterLinqGroupingBase)Activator.CreateInstance(typeof(InterLinqGrouping <,>).MakeGenericType(genericTypes));

            newGrouping.SetKey(key);
            newGrouping.SetElements(elements);
            return(newGrouping);
        }