コード例 #1
0
        static SpecializedEnumerableElements()
        {
            foreach (var ctorInfo in typeof(TCollection).GetConstructors())
            {
                var ctorParams = ctorInfo.GetParameters();
                if (ctorParams.Length != 1)
                {
                    continue;
                }
                var ctorParam = ctorParams[0];

                if (typeof(IEnumerable).IsAssignableFrom(ctorParam.ParameterType) ||
                    ctorParam.ParameterType.IsOrHasGenericInterfaceTypeOf(typeof(IEnumerable <>)))
                {
                    ConvertFn = fromObject =>
                    {
                        var to = Activator.CreateInstance(typeof(TCollection), fromObject);
                        return(to);
                    };
                    return;
                }
            }

            if (typeof(TCollection).IsOrHasGenericInterfaceTypeOf(typeof(ICollection <>)))
            {
                ConvertFn = ConvertFromCollection;
            }
        }
コード例 #2
0
        static SpecializedEnumerableElements()
        {
            foreach (var ctorInfo in typeof(TCollection).DeclaredConstructors())
            {
                var ctorParams = ctorInfo.GetParameters();
                if (ctorParams.Length != 1)
                {
                    continue;
                }
                var ctorParam = ctorParams[0];

                if (typeof(IEnumerable).AssignableFrom(ctorParam.ParameterType) ||
                    ctorParam.ParameterType.IsOrHasGenericInterfaceTypeOf(typeof(IEnumerable <>)))
                {
                    ConvertFn = fromObject =>
                    {
#if !NETCF
                        var to = Activator.CreateInstance(typeof(TCollection), fromObject);
#else
                        var to = typeof(TCollection).GetConstructor(new Type[] { fromObject.GetType() }).Invoke(new[] { fromObject });
#endif
                        return(to);
                    };
                    return;
                }
            }

            if (typeof(TCollection).IsOrHasGenericInterfaceTypeOf(typeof(ICollection <>)))
            {
                ConvertFn = ConvertFromCollection;
            }
        }