Пример #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
        /// <summary>
        /// Returns the CLR <see cref="MemberInfo"/>.
        /// </summary>
        /// <returns>Returns the CLR <see cref="MemberInfo"/>.</returns>
        public override MemberInfo GetClrVersion()
        {
            InterLinqTypeSystem tsInstance = InterLinqTypeSystem.Instance;

            lock (tsInstance)
            {
                if (tsInstance.IsInterLinqMemberInfoRegistered(this))
                {
                    return(tsInstance.GetClrVersion <MethodInfo>(this));
                }

                Type       declaringType        = (Type)DeclaringType.GetClrVersion();
                Type[]     genericArgumentTypes = GenericArguments.Select(p => (Type)p.GetClrVersion()).ToArray();
                MethodInfo foundMethod          = null;
                foreach (MethodInfo method in declaringType.GetMethods().Where(m => m.Name == Name))
                {
                    MethodInfo currentMethod = method;
                    if (currentMethod.IsGenericMethod)
                    {
                        if (currentMethod.GetGenericArguments().Length == genericArgumentTypes.Length)
                        {
                            currentMethod = currentMethod.MakeGenericMethod(genericArgumentTypes);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    ParameterInfo[] currentParameters = currentMethod.GetParameters();
                    if (ParameterTypes.Count == currentParameters.Length)
                    {
                        bool allArumentsFit = true;
                        for (int i = 0; i < ParameterTypes.Count && i < currentParameters.Length; i++)
                        {
                            Type currentArg       = (Type)ParameterTypes[i].GetClrVersion();
                            Type currentParamType = currentParameters[i].ParameterType;
                            if (!currentParamType.IsAssignableFrom(currentArg))
                            {
                                allArumentsFit = false;
                                break;
                            }
                        }
                        if (allArumentsFit)
                        {
                            foundMethod = currentMethod;
                        }
                    }
                }

                if (foundMethod == null)
                {
                    throw new Exception(string.Format("Method \"{0}.{1}\" not found.", declaringType, Name));
                }
                tsInstance.SetClrVersion(this, foundMethod);
                return(foundMethod);
            }
        }
Пример #3
0
        /// <summary>
        /// Converts an <see langword="object"/> into a target <see cref="Type"/>.
        /// </summary>
        /// <param name="wantedType">Target <see cref="Type"/>.</param>
        /// <param name="objectToConvert"><see langword="object"/> to convert.</param>
        /// <returns>Returns the converted <see langword="object"/>.</returns>
        public static object ConvertFromSerializable(Type wantedType, object objectToConvert)
        {
            if (objectToConvert == null)
            {
                return(null);
            }
            if (wantedType.IsIGrouping() && objectToConvert is InterLinqGroupingBase)
            {
                Type[]     genericType = objectToConvert.GetType().GetGenericArguments();
                MethodInfo method      = typeof(TypeConverter).GetMethod("ConvertFromInterLinqGrouping", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(genericType);
                return(method.Invoke(null, new[] { wantedType, objectToConvert }));
            }
            Type wantedElementType = InterLinqTypeSystem.FindIEnumerable(wantedType);

            if (wantedElementType != null && wantedElementType.GetGenericArguments()[0].IsAnonymous())
            {
                Type typeOfObject = objectToConvert.GetType();
                Type elementType  = InterLinqTypeSystem.FindIEnumerable(typeOfObject);
                if (elementType != null && elementType.GetGenericArguments()[0] == typeof(AnonymousObject))
                {
                    MethodInfo method = typeof(TypeConverter).GetMethod("ConvertFromSerializableCollection", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(wantedElementType.GetGenericArguments()[0]);
                    return(method.Invoke(null, new[] { objectToConvert }));
                }
            }
            if (wantedType.IsAnonymous() && objectToConvert is AnonymousObject)
            {
                AnonymousObject   dynamicObject = (AnonymousObject)objectToConvert;
                List <object>     properties    = new List <object>();
                ConstructorInfo[] constructors  = wantedType.GetConstructors();
                if (constructors.Length != 1)
                {
                    throw new Exception("Usualy, anonymous types have just one constructor.");
                }
                ConstructorInfo constructor = constructors[0];
                foreach (ParameterInfo parameter in constructor.GetParameters())
                {
                    object propertyValue      = null;
                    bool   propertyHasBeenSet = false;
                    foreach (AnonymousProperty dynProperty in dynamicObject.Properties)
                    {
                        if (dynProperty.Name == parameter.Name)
                        {
                            propertyValue      = dynProperty.Value;
                            propertyHasBeenSet = true;
                            break;
                        }
                    }
                    if (!propertyHasBeenSet)
                    {
                        throw new Exception(string.Format("Property {0} could not be found in the dynamic object.", parameter.Name));
                    }
                    properties.Add(ConvertFromSerializable(parameter.ParameterType, propertyValue));
                }
                return(constructor.Invoke(properties.ToArray()));
            }
            return(objectToConvert);
        }
Пример #4
0
        /// <summary>
        /// Returns the CLR <see cref="MemberInfo"/>.
        /// </summary>
        /// <returns>Returns the CLR <see cref="MemberInfo"/>.</returns>
        public override MemberInfo GetClrVersion()
        {
            InterLinqTypeSystem tsInstance = InterLinqTypeSystem.Instance;

            lock (tsInstance)
            {
                if (tsInstance.IsInterLinqMemberInfoRegistered(this))
                {
                    return(tsInstance.GetClrVersion <Type>(this));
                }
                Type createdType = CreateClrType();
                tsInstance.SetClrVersion(this, createdType);
                return(createdType);
            }
        }
Пример #5
0
        /// <summary>
        /// Returns the CLR <see cref="MemberInfo"/>.
        /// </summary>
        /// <returns>Returns the CLR <see cref="MemberInfo"/>.</returns>
        public override MemberInfo GetClrVersion()
        {
            InterLinqTypeSystem tsInstance = InterLinqTypeSystem.Instance;

            lock (tsInstance)
            {
                if (tsInstance.IsInterLinqMemberInfoRegistered(this))
                {
                    return(tsInstance.GetClrVersion <ConstructorInfo>(this));
                }

                Type            declaringType    = (Type)DeclaringType.GetClrVersion();
                ConstructorInfo foundConstructor = declaringType.GetConstructor(ParameterTypes.Select(p => (Type)p.GetClrVersion()).ToArray());
                tsInstance.SetClrVersion(this, foundConstructor);
                return(foundConstructor);
            }
        }
Пример #6
0
        /// <summary>
        /// Returns the CLR <see cref="MemberInfo"/>.
        /// </summary>
        /// <returns>Returns the CLR <see cref="MemberInfo"/>.</returns>
        public override MemberInfo GetClrVersion()
        {
            InterLinqTypeSystem tsInstance = InterLinqTypeSystem.Instance;

            lock (tsInstance)
            {
                if (tsInstance.IsInterLinqMemberInfoRegistered(this))
                {
                    return(tsInstance.GetClrVersion <FieldInfo>(this));
                }

                Type      declaringType = (Type)DeclaringType.GetClrVersion();
                FieldInfo foundField    = declaringType.GetField(Name);
                tsInstance.SetClrVersion(this, foundField);
                return(foundField);
            }
        }
Пример #7
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);
        }
        /// <summary>
        /// Returns the CLR <see cref="MemberInfo"/>.
        /// </summary>
        /// <returns>Returns the CLR <see cref="MemberInfo"/>.</returns>
        public override MemberInfo GetClrVersion()
        {
            InterLinqTypeSystem tsInstance = InterLinqTypeSystem.Instance;

            lock (tsInstance)
            {
                if (tsInstance.IsInterLinqMemberInfoRegistered(this))
                {
                    return(tsInstance.GetClrVersion <ConstructorInfo>(this));
                }

#if !NETFX_CORE
                Type            declaringType    = (Type)DeclaringType.GetClrVersion();
                ConstructorInfo foundConstructor = declaringType.GetConstructor(ParameterTypes.Select(p => (Type)p.GetClrVersion()).ToArray());
#else
                Type            declaringType    = ((TypeInfo)DeclaringType.GetClrVersion()).AsType();
                ConstructorInfo foundConstructor = declaringType.GetTypeInfo().DeclaredConstructors.FirstOrDefault(x => Enumerable.SequenceEqual(x.GetParameters().Select(y => y.ParameterType), ParameterTypes.Select(p => ((TypeInfo)p.GetClrVersion()).AsType())));
#endif
                tsInstance.SetClrVersion(this, foundConstructor);
                return(foundConstructor);
            }
        }
Пример #9
0
        /// <summary>
        /// Returns the CLR <see cref="MemberInfo"/>.
        /// </summary>
        /// <returns>Returns the CLR <see cref="MemberInfo"/>.</returns>
        public override MemberInfo GetClrVersion()
        {
            InterLinqTypeSystem tsInstance = InterLinqTypeSystem.Instance;

            lock (tsInstance)
            {
                if (tsInstance.IsInterLinqMemberInfoRegistered(this))
                {
                    return(tsInstance.GetClrVersion <FieldInfo>(this));
                }

#if !NETFX_CORE
                Type      declaringType = (Type)DeclaringType.GetClrVersion();
                FieldInfo foundField    = declaringType.GetField(Name);
#else
                Type      declaringType = ((TypeInfo)DeclaringType.GetClrVersion()).AsType();
                FieldInfo foundField    = declaringType.GetTypeInfo().DeclaredFields.FirstOrDefault(x => x.Name == Name);
#endif
                tsInstance.SetClrVersion(this, foundField);
                return(foundField);
            }
        }
Пример #10
0
        /*private static object ConvertFromInterLinqGrouping<TKey, TElement>(Type wantedType, InterLinqGrouping<TKey, TElement> grouping)
         * {
         *  Type[] genericArguments = wantedType.GetGenericArguments();
         *  object key = ConvertFromSerializable(genericArguments[0], grouping.Key);
         *
         *  MethodInfo method = typeof(TypeConverter).GetMethod("ConvertFromSerializableCollection", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(genericArguments[1]);
         *  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.");
         *  }
         *  Type[] genericTypes = new[] { key.GetType(), elementType.GetGenericArguments()[0] };
         *  InterLinqGroupingBase newGrouping = (InterLinqGroupingBase)Activator.CreateInstance(typeof(InterLinqGrouping<,>).MakeGenericType(genericTypes));
         *  newGrouping.SetKey(key);
         *  newGrouping.SetElements(elements);
         *  return newGrouping;
         * }*/

        /* /// <summary>
         * /// Converts each element of an <see cref="IEnumerable"/>
         * /// into a target <see cref="Type"/>.
         * /// </summary>
         * /// <typeparam name="T">Target <see cref="Type"/>.</typeparam>
         * /// <param name="enumerable"><see cref="IEnumerable"/>.</param>
         * /// <returns>Returns the converted <see cref="IEnumerable"/>.</returns>
         * private static IEnumerable ConvertFromSerializableCollection<T>(IEnumerable enumerable)
         * {
         *   Type enumerableType = typeof(List<>).MakeGenericType(typeof(T));
         *   IEnumerable newList = (IEnumerable)Activator.CreateInstance(enumerableType);
         *   MethodInfo addMethod = enumerableType.GetMethod("Add");
         *   foreach (object item in enumerable)
         *   {
         *       addMethod.Invoke(newList, new[] { ConvertFromSerializable(typeof(T), item) });
         *   }
         *   return newList;
         * }*/


        #endregion

        #region Convert C# Anonymous Type to AnonymousObject

        /// <summary>
        /// Converts an object to an <see cref="AnonymousObject"/>
        /// or an <see cref="IEnumerable{AnonymousObject}"/>.
        /// </summary>
        /// <param name="objectToConvert"><see langword="object"/> to convert.</param>
        /// <returns>Returns the converted <see langword="object"/>.</returns>
        public static object ConvertToSerializable(object objectToConvert)
        {
            if (objectToConvert == null)
            {
                return(null);
            }

            Type typeOfObject = objectToConvert.GetType();
            Type elementType  = InterLinqTypeSystem.FindIEnumerable(typeOfObject);

            // Handle "IGrouping<TKey, TElement>"
            if (typeOfObject.IsIGrouping())
            {
                Type[]     genericType = typeOfObject.GetGenericArguments();
                MethodInfo method      = typeof(TypeConverter).GetMethod("ConvertToInterLinqGrouping", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(genericType);
                return(method.Invoke(null, new[] { objectToConvert }));
            }
            // Handle "IEnumerable<AnonymousType>" / "IEnumerator<T>"
            if (elementType != null && elementType.GetGenericArguments()[0].IsAnonymous() || typeOfObject.IsEnumerator())
            {
                // ReSharper disable PossibleNullReferenceException
                MethodInfo method = typeof(TypeConverter).GetMethod("ConvertToSerializableCollection", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(elementType.GetGenericArguments()[0]);
                // ReSharper restore PossibleNullReferenceException
                return(method.Invoke(null, new[] { objectToConvert }));
            }
            // Handle "AnonymousType"
            if (typeOfObject.IsAnonymous())
            {
                AnonymousObject newObject = new AnonymousObject();
                foreach (PropertyInfo property in typeOfObject.GetProperties(BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.Public))
                {
                    object objectValue = ConvertToSerializable(property.GetValue(objectToConvert, new object[] { }));
                    newObject.Properties.Add(new AnonymousProperty(property.Name, objectValue));
                }
                return(newObject);
            }

            return(objectToConvert);
        }
        /// <summary>
        /// Returns the CLR <see cref="MemberInfo"/>.
        /// </summary>
        /// <returns>Returns the CLR <see cref="MemberInfo"/>.</returns>
        public override MemberInfo GetClrVersion()
        {
            InterLinqTypeSystem tsInstance = InterLinqTypeSystem.Instance;

            lock (tsInstance)
            {
                if (tsInstance.IsInterLinqMemberInfoRegistered(this))
                {
                    return(tsInstance.GetClrVersion <PropertyInfo>(this));
                }

#if !NETFX_CORE
                Type         declaringType = (Type)DeclaringType.GetClrVersion();
                PropertyInfo foundProperty = declaringType.GetProperty(Name);
#else
                Type         declaringType = ((TypeInfo)DeclaringType.GetClrVersion()).AsType();
                PropertyInfo foundProperty = declaringType.GetTypeInfo().GetDeclaredProperty(Name);
#endif
                tsInstance.SetClrVersion(this, foundProperty);
                return(foundProperty);
            }
        }
Пример #12
0
        /// <summary>
        /// Returns the CLR <see cref="MemberInfo"/>.
        /// </summary>
        /// <returns>Returns the CLR <see cref="MemberInfo"/>.</returns>
        public override MemberInfo GetClrVersion()
        {
            InterLinqTypeSystem tsInstance = InterLinqTypeSystem.Instance;

            lock (tsInstance)
            {
                if (tsInstance.IsInterLinqMemberInfoRegistered(this))
                {
#if !NETFX_CORE
                    return(tsInstance.GetClrVersion <Type>(this));
#else
                    return(tsInstance.GetClrVersion <TypeInfo>(this));
#endif
                }
                Type createdType = CreateClrType();
#if !NETFX_CORE
                tsInstance.SetClrVersion(this, createdType);
                return(createdType);
#else
                tsInstance.SetClrVersion(this, createdType.GetTypeInfo());
                return(createdType.GetTypeInfo());
#endif
            }
        }
Пример #13
0
        /// <summary>
        /// Converts an object to an <see cref="AnonymousObject"/>
        /// or an <see cref="IEnumerable{AnonymousObject}"/>.
        /// </summary>
        /// <param name="objectToConvert"><see langword="object"/> to convert.</param>
        /// <returns>Returns the converted <see langword="object"/>.</returns>
        public static object ConvertToSerializable(object objectToConvert)
        {
            if (objectToConvert == null)
            {
                return(null);
            }

            Type typeOfObject = objectToConvert.GetType();
            Type elementType  = InterLinqTypeSystem.FindIEnumerable(typeOfObject);

            // Handle "IGrouping<TKey, TElement>"
            if (typeOfObject.IsIGrouping())
            {
#if !NETFX_CORE
                Type[] genericType = typeOfObject.GetGenericArguments();
#else
                Type[] genericType = typeOfObject.GetTypeInfo().GenericTypeArguments;
#endif
#if !SILVERLIGHT
                MethodInfo method = typeof(TypeConverter).GetMethod("ConvertToInterLinqGrouping", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(genericType);
#else
#if !NETFX_CORE
                MethodInfo method = typeof(TypeConverter).GetMethod("ConvertToInterLinqGrouping", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(genericType);
#else
                MethodInfo method = typeof(TypeConverter).GetTypeInfo().GetDeclaredMethod("ConvertToInterLinqGrouping").MakeGenericMethod(genericType);
#endif
#endif
                return(method.Invoke(null, new[] { objectToConvert }));
            }

            // Handle "IGrouping<TKey, TElement>[]"
            if (typeOfObject.IsIGroupingArray())
            {
#if !NETFX_CORE
                Type[] genericType = typeOfObject.GetGenericArguments();
#else
                Type[] genericType = typeOfObject.GetTypeInfo().GenericTypeArguments;
#endif
#if !SILVERLIGHT
                MethodInfo method = typeof(TypeConverter).GetMethod("ConvertToInterLinqGroupingArray", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(genericType);
#else
#if !NETFX_CORE
                MethodInfo method = typeof(TypeConverter).GetMethod("ConvertToInterLinqGroupingArray", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(genericType);
#else
                MethodInfo method = typeof(TypeConverter).GetTypeInfo().GetDeclaredMethod("ConvertToInterLinqGroupingArray").MakeGenericMethod(genericType);
#endif
#endif
                return(method.Invoke(null, new[] { objectToConvert }));
            }

            // Handle "IEnumerable<AnonymousType>" / "IEnumerator<T>"
#if !NETFX_CORE
            if (elementType != null && elementType.GetGenericArguments()[0].IsAnonymous() || typeOfObject.IsEnumerator())
#else
            if (elementType != null && elementType.GetTypeInfo().GenericTypeArguments[0].IsAnonymous() || typeOfObject.IsEnumerator())
#endif
            {
#if !SILVERLIGHT
                MethodInfo method = typeof(TypeConverter).GetMethod("ConvertToSerializableCollection", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(elementType.GetGenericArguments()[0]);
#else
#if !NETFX_CORE
                MethodInfo method = typeof(TypeConverter).GetMethod("ConvertToSerializableCollection", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(elementType.GetGenericArguments()[0]);
#else
                MethodInfo method = typeof(TypeConverter).GetTypeInfo().GetDeclaredMethod("ConvertToSerializableCollection").MakeGenericMethod(elementType.GetTypeInfo().GenericTypeArguments[0]);
#endif
#endif
                return(method.Invoke(null, new[] { objectToConvert }));
            }
            // Handle "AnonymousType"
            if (typeOfObject.IsAnonymous())
            {
                AnonymousObject newObject = new AnonymousObject();
#if !NETFX_CORE
                foreach (PropertyInfo property in typeOfObject.GetProperties(BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.Public))
#else
                foreach (PropertyInfo property in typeOfObject.GetTypeInfo().DeclaredProperties)
#endif
                {
                    object objectValue = ConvertToSerializable(property.GetValue(objectToConvert, new object[] { }));
                    newObject.Properties.Add(new AnonymousProperty(property.Name, objectValue));
                }
                return(newObject);
            }

            return(objectToConvert);
        }